Search code examples
entity-frameworkentity-framework-4

Querying objects after AddObject before SaveChanges?


In EntityFramework, is that possible to query the objects that have just been added to the context using AddObject but before calling the SaveChanges method?

Thanks


Solution

  • you can query objects like this,

    context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).Select(obj => obj.Entity).OfType<TheEntityType>()
    

    this will query the objects which are in added state. If you want other states too you can pass all other states to GetObjectStateEntries method like this.

    GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Unchanged)