views:

219

answers:

3

Bit of an odd question but is there a way of seeing what objects are attached to my object context. I am getting a few random problems and it would really be helpful to solve them if i could see what's been attached and not yet saved through "SaveChanges".

Answer (Entity Framework) : context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(o => o.Entity).OfType<YourObjectType>();

+1  A: 

I think this article might be of interest.

It covers using reflection to look inside (private) fields in the DataContext for changed items. I believe it could be adapted to show all items, not just changed ones.

Adam
Many thanks, i will read it first thing tomorrow morning at work. Sounds like it should help me out.
Kohan
That's for LINQ to SQL only. It won't work for EF. (Though I see this question has been tagged LINQ to SQL in the past.)
Craig Stuntz
+1  A: 

Maybe I'm misunderstanding (or oversimplifying) your question but it sounds like GetChangeSet() could help you?

Smashd
Thanks, your suggestion lead to a relativly simple solution. GetChangeSet only works with linq to sql and i am using Entities but i found this post: http://stackoverflow.com/questions/326186/is-query-to-new-added-object-possible-in-ms-entity-framework , and it really helped me out with "context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged).Select(o => o.Entity).OfType<YourObjectType>()" which can be used in EF.
Kohan
A: 

You look at the object state entries in the ObjectStateManager. This article has an example.

Craig Stuntz