3

Is there any way to detect changes made in last ObjectContext.SaveChanges() method?

I track the changes myself like update delete and insert of objects, but when there is foreign key relationship, entity framework deletes child references automatically and here I loose tracking.

So for example if I delete a student I track that I have deleted one student.

But when I delete class, it automatically deletes all the students from the class as I have foreign key relationship for them. The problem is I can only track here that one class is deleted but not how many students in the class were deleted.

One way is to count the students of class before deleting it, but that is an unnecessary trip to database.

Is there any other way?

4

1 回答 1

2

您可以使用 上的ChangeTracker属性DbContext来找出实体框架在SaveChanges被调用时会做什么。如果您在调用之前SaveChanges检查它,您将获得 Entity Framework 将要删除或修改的所有内容。

这假设确实是实体框架执行级联删除。它也可能被实现为数据库中外键的级联删除选项。在这种情况下,执行删除的是数据库而不是 EF,因此更改跟踪器不会知道待处理的删除。

于 2013-06-22T11:44:22.250 回答