tags:

views:

326

answers:

3

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 :-)

A: 

Look at the ObjectTracker or something that hangs off DataContext. It has the change list stored in there.

Also, I believe SubmitChanges is virtual, so you can intercept the SubmitChanges call and do some checking there.

CVertex
+1  A: 

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.

Marc Gravell
A: 

I if I understand the question correctly...

These show you what the DataContext is tracking

DataContext.GetChangeSet().Inserts;
DataContext.GetChangeSet().Deletes;
DataContext.GetChangeSet().Updates;

Is this what your thinking?

if (DataContext.GetChageSet().Inserts.Count = 0
        && DataContext.GetChageSet().Deletes.Count
        && DataContext.GetChageSet().Updates.Count)
{
    throw new Exception("You forgot to do something with your DataContext...");
}
else
{
    DataContext.SubmitChanges();
}
J.13.L
nop, the OP wants the count of tracked objects regardless of whether they have changed.
eglasius
wouldn't that just be DataContext.YourEntities.Count()? I don't understand what you mean by tracked... The DataContext keeps track of all of the objects in the database.
J.13.L