C# – check wheter Linq 2 SQL’s DataContext is tracking entities

clinq-to-sql

We want to throw an exception, if a user calls DataContext.SubmitChanges() and the DataContext is not tracking anything.

That is… it is OK to call SubmitChanges if there are no inserts, updates or deletes. But we want to ensure that the developer didn't forget to attach the entity to the DataContext.

Even better… is it possible to get a collection of all entities that the DataContext is tracking (including those that are not changed)?

PS: The last question I asked were answered with: "do it this way instead"… please don't 🙂

Best Answer

I don't think there are any public/protected methods that would let you get at this directly. You'd probably have to use reflection, like I did about 3 messages down here, looking at the ChangeTracker property of the Services. Very ugly, I'm afraid.

Related Topic