Is there a way to add object to de dataContext without committing the changes, when I use
ctx.Articles.AddObject(newart);
the newart doesn't appear inth e context ctx, the only way i was able to do that is by saveing changes ctx.SaveChanges , unfortunatly that save changes to the DB, & i don't want to, is there a way to do that, Any help will be appreciated
May work in EF4
You should be able to get all currently tracked objects via the ObjectStateManager of your ObjectContext like
var articles = ctx.ObjectStateManager
// Get all entries for added/changed/modified entities
.GetObjectStateEntries(EntityState.Added |
EntityState.Unchanged |
EntityState.Modified)
// select the entity objects from the entries
.Select(entry=>entry.Entity)
// we're only interested in Article objects
.OfType<Article>();
Note: Since I haven't installed EF4 so I couldn't verify this solution. But I remember darkly having done this successfully some time ago :). It might not be very performant when done with very large object sets though ...
Works for EF 5
You can use theLocal Property of the DbSet class. This property represents the object currently tracked by the Context (i.e. all objects that are currently loaded and/or locally added without those being deleted). Accessing the Local property never does a DB query.
So you can use ctx.Articles.Local and get a data set which contains your newart object (along with any objects loaded by previous queries)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With