学习011-03-16 Resolve SessionMixingException: “The object that has been passed belongs to another Obje

Resolve SessionMixingException: “The object that has been passed belongs to another ObjectSpace. (An error with number 1021 has occurred.)”(解决 SessionMixingException:“已传递的对象属于另一个对象空间。(发生了编号为 1021 的错误。)”)

Scenario(情景)

A View Controller contains the AddTaskAction SimpleAction that implements the following logic:
视图控制器包含实现以下逻辑的AddTaskAction SimpleAction:

C#
private void AddTaskAction_Execute(Object sender, SimpleActionExecuteEventArgs e){  
    IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Contact));  
    Contact contact = objectSpace.CreateObject<Contact>();  
    Task task = (Task)View.CurrentObject;  
    contact.Tasks.Add(task);  
    e.ShowViewParameters.CreatedView = Application.CreateDetailView(objectSpace, contact, true);  
}

When XAF executes the AddTaskAction action, the Session Mixing Exception is raised: An error with number 1021 has occurred. Error message: The object that has been passed belongs to another ObjectSpace…
当XAF执行AddTaskAction操作时,会引发会话混合异常:An发生编号为1021的错误。错误消息:已传递的对象属于另一个ObjectSpace…

Explanation(说明)

The XPO Session/UnitOfWork or XAF Object Space is a separate data context. (ObjectSpace wraps the Session/UnitOfWork.) The Session Mixing Exception occurs if you try to create a link between two objects from different Sessions or Object Spaces, for example:
XPO Session/UnitOfWork或XAF对象空间是一个单独的数据上下文。(ObjectSpace包装了Session/UnitOfWork。)如果您尝试在来自不同会话或对象空间的两个对象之间创建链接,则会发生会话混合异常,例如:

  • Assign an object to a reference property of an object from another Session (Object Space);
    将对象分配给来自另一个会话(对象空间)的对象的引用属性;
  • Add objects from different Sessions (Object Spaces) to one collection;
    将来自不同会话(对象空间)的对象添加到一个集合中;
  • Display objects from one Session (Object Space) in a View created in another Session or Object Space.
    在另一个会话或对象空间中创建的视图中显示来自一个会话(对象空间)的对象。

The code above raises the Session Mixing Exception in the AddTaskAction_Execute method on an attempt to add a new Task to the Contact.Tasks collection. The exception is thrown because the Contact object is created in a new ObjectSpace, while the Task instance belongs to the current View Object Space.
上面的代码在AddTaskAction_Execute方法中引发会话混合异常,以尝试将新任务添加到Contact。任务集合。引发异常是因为联系人对象是在新的ObjectSpace中创建的,而任务实例属于当前的View Object Space。

Solutions(解决方案)

Get An Existing Object from Another Data Context(从另一个数据上下文获取现有对象)

To avoid the Session Mixing Exception, manipulate all related objects within the same data context (the same Session or Object Space).
为避免会话混合异常,请在相同的数据上下文(相同的会话或对象空间)中操作所有相关对象。

XPO

To get an object from one data context in another data context, perform the steps below:
要从另一个数据上下文中的一个数据上下文中获取对象,请执行以下步骤:

  • Call the Session.GetKeyValue method to get the object ID.
    调用Session. GetKeyValue方法获取对象ID。
  • Use the GetObjectByKey or FindObject method to get a single saved object from a database.
    使用GetObjectByKey或FindObject方法从数据库中获取单个保存的对象。
C#
object taskKey = ((Task)View.CurrentObject).Session.GetKeyValue(View.CurrentObject);
contact.Tasks.Add(session.GetObjectByKey<Task>(taskKey));

XAF
In XAF applications, you can use the IObjectSpace.GetObject method that accepts an object from another data context. The following example shows how to use the IObjectSpace.GetObject method to load an existing Task object (the current View Object Space) to a newly created Object Space.
在XAF应用程序中,您可以使用IObjectSpace. GetObject方法从另一个数据上下文中接受对象。以下示例显示了如何使用IObjectSpace.GetObject方法将现有的任务对象(当前的视图对象空间)加载到新创建的对象空间。

C#
private void AddTaskAction_Execute(Object sender, SimpleActionExecuteEventArgs e){  
    IObjectSpace objectSpace = Application.CreateObjectSpace(typeof(Contact));  
    Contact contact = objectSpace.CreateObject<Contact>();  
    Task task = (Task)View.CurrentObject;  
    contact.Tasks.Add(objectSpace.GetObject(task));  
    e.ShowViewParameters.CreatedView = Application.CreateDetailView(objectSpace, contact, true);
}

Get A Newly Created Object from Another Data Context(从另一个数据上下文获取新创建的对象)

Newly created and unsaved objects cannot be obtained from a database. Use the following techniques to get unsaved objects:
无法从数据库中获取新创建和未保存的对象。使用以下技术获取未保存的对象:

  • Load related objects in the new object data context. Use the View.ObjectSpace property to get the new object data context.
    在新的对象数据上下文中加载相关对象。使用View. ObjectSpace属性获取新的对象数据上下文。
  • Create a nested Session (Object Space) that allows you to transfer unsaved objects between a nested data context and its parent. Refer to the following articles for more information:
    创建一个嵌套会话(对象空间),允许您在嵌套数据上下文及其父级之间传输未保存的对象。有关更多信息,请参阅以下文章:
    • Nested Units of Work (XPO)
    • XPObjectSpace.CreateNestedObjectSpace (XAF)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值