Reputation: 8960
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?
Upvotes: 3
Views: 447
Reputation: 69260
You can use the ChangeTracker
property on the DbContext
to find out what Entity Framework will do when SaveChanges
is called. If you check it before you call SaveChanges
you will get everything that Entity Framework is about to delete or modify.
This assumes that it is indeed Entity Framework that does the cascading delete. It might also have been implemented as a cascade delete option on the foreign key in the database. In that case, it's the DB doing the deletes and not EF so the change tracker will not be aware of the pending deletions.
Upvotes: 2