在另一个SessionScope中正确使用DifferentDatabaseScope

时间:2012-03-08 17:29:26

标签: castle-activerecord

我有以下代码(为简洁而简化)

[ActiveRecord(Table = "[Order]"),
Serializable]
public class Order : ActiveRecordLinqBase<Order>, INotifyPropertyChanged
{
            //Other properties omitted for brevity

    private IList<Reading> _readings;
    [HasMany(Inverse = true, Cascade = ManyRelationCascadeEnum.All)]
    public virtual IList<Reading> Readings
    {
        get { return _readings; }
        set
        {
            _readings = value;

            DoPropertyChanged(() => this.Readings);
        }
    }
}



using (new SessionScope(FlushAction.Never) {
    Order o = _repository.GetOrder(OrderId);
    bool result = ShowReadings(this, o);
    if (result) s.Flush();
}


bool ShowReadings(Window owner, Order o) {
    //populate a datagrid with Reading objects from Order
    this.Readings = Order.Readings  //datagrid binds to Readings property of the form

    if (cbxHistoricalChecked) {
        using(new DifferentDatabaseScope(SomeOtherConnectionString)) {
            //bind datagrid to historical readings
            this.Readings = GetHistoricalReadings(Order.OrderId);
        }
    }

    if (SignedOff) {
        using(var t = new TransactionScope()) {
           //update some properties on the Order
           t.VoteCommit();
        }
    }
    return true;
}

所以我正在做的是在读数表单打开时打开SessionScope,并在关闭后刷新。如果用户想要查看订单的历史读数,我必须为历史读数打开不同的数据库。 只要我不打开DifferentDatabaseScope,事情就可以正常工作,但是当我这样做时,新的TransactionScope的创建就会失败: Castle.ActiveRecord.Framework.Scopes.ScopeMachineryException {&#34;尝试取消注册非活动范围&#34;}

是否有可能做我想做的事情,如果是的话,做正确的方法是什么?

0 个答案:

没有答案