自跟踪实体 - 从导航属性中删除不会将状态设置为“已删除”;
在我的每个 POCO 的构造函数中,我都有这样的内容:
this.StartTracking();
确保为我的 POCO 之一的每个实例打开跟踪。我有一个实体 A,其中包含实体 B 的 TrackableCollection。当我像这样加载实体 A 的实例时:
using(MyContext ctx = new MyContext())
{
entityA = ctx.EntityA.Include("EntityB").Where(x => x.Id== id).FirstOrDefault();
}
查看实体 A 上的 ObjectsAddedToCollection 属性,有 1 个对象标记为“未更改”。当我执行entityA.EntityB[0].MarkAsDeleted()时,状态不会设置为“已删除”并移动到ObjectsRemovedFromCollection集合。它只是被完全删除。我仔细检查了一下,实体 A 和实体 B 的 ChangeTrackingEnabled 都设置为 True。这不起作用有什么原因吗?因此,我无法删除子实体并将更改保留到数据库。
In the constructor of each of my POCO's I have this:
this.StartTracking();
To ensure that tracking is turned on for every instance of one of my POCO's. I have an Entity A which contains a TrackableCollection of Entity B. When I load my instance of Entity A like such:
using(MyContext ctx = new MyContext())
{
entityA = ctx.EntityA.Include("EntityB").Where(x => x.Id== id).FirstOrDefault();
}
Looking at the ObjectsAddedToCollection property on Entity A, there is 1 object flagged as 'Unchanged'. When I do entityA.EntityB[0].MarkAsDeleted(), the state does not get set to 'Deleted' and moved to the ObjectsRemovedFromCollection collection. It just gets removed altogether. I double checked and the ChangeTrackingEnabled is set to True for both Entity A and Entity B. Is there a reason why this is not working? Because of this I cannot delete child entity and persist the changes to the database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎是一个错误。在 RecordRemovalFromCollectionProperties 方法中,我更改了仅返回到的行:
这解决了我遇到的问题。
This appears to be a bug. In the method RecordRemovalFromCollectionProperties I changed the line that simply returns to:
This fixed the issues I was having.
尽管更改此生成的代码非常危险。
RecordRemovalFromCollectionProperties
方法的作用是查看是否有添加的对象再次被删除(最终状态=没有添加或删除实体,添加和删除“互相删除”,你看...... ?)。这就是为什么还有一个 RecordAdditionToCollectionProperties 来对我之前解释的内容进行“反向”检查。现在,通过更改 ChangeTracker 的此方法,您可能会发送一个具有添加的 EntityB 和删除的 EntityB(它们是相同的实例)的 EntityA。这可以由用户或通过代码以某种方式完成。
我不知道对象上下文是否首先允许这样做。但它至少有点低效。发送一个更新的 EntityA,它对对象上下文 (
context.ApllyChanges(EntityA)
) 添加这个 EntityB,然后立即删除这个相同的 EntityB ;)我在这个领域有一些经验,所以如果你还有进一步的问题...
我认为原来的问题是有原因的,但你的解决方案有点粗糙。
Pretty dangerous though by changing this generated code. What the
RecordRemovalFromCollectionProperties
method does is to see if there are added objects that are removed again (final state = no entities added or removed, the adding and removal "strike out each other", you see...?). That's why there's also aRecordAdditionToCollectionProperties
which does the "inverse" check of what I explained before.Now, with your change to this method of the ChangeTracker it could be possible that you will sent away an EntityA that has an added EntityB and a removed EntityB (which are the same instances). This could be done by a user or by code someway.
I don't know if the object context will allow this at first. But it's at least a little inefecient. Sending an updated EntityA which says to the object context (
context.ApllyChanges(EntityA)
) Add this EntityB and immediately afterwards, mwoah remove this same EntityB also ;)I have some experience in this field, so If you have further questions...
I think the original problem has a reason, but that your solution is a little rough.