Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework How Do I Detach All Objects Of Specific Type From Datacontext?

Currently I am trying to implement code prior to my datacontext.savechanges() method that detaches all objects of a specific type from the data context so that they do not get inserted into the database. I know that you can call dbContext.Entry(entity).State = EntityState.Detached to detach a single entity, but is there a way to detach all objects of a specific type from the context?

Edit: I implemented what Mark C suggested and it compiles, but I get an invalid operation exception when I try to execute the code:

The entity type DbEntityEntry is not part of the model for the current context.

   foreach (System.Data.Entity.Infrastructure.DbEntityEntry dbEntityEntry in this.dataContext.ChangeTracker.Entries<Customer>())
            {
                if (dbEntityEntry != null && dbEntityEntry.State != System.Data.Entity.EntityState.Modified && dbEntityEntry.State != System.Data.Entity.EntityState.Unchanged)
                {
                    dataContext.Entry(dbEntityEntry).State = System.Data.Entity.EntityState.Detached;
                }
            }

Edit #2: After looking at it more closely, I was making a mistake. It appears one needs to specify the entity on DBEntityEntry. With the following change it works:

        foreach (System.Data.Entity.Infrastructure.DbEntityEntry dbEntityEntry in this.dataContext.ChangeTracker.Entries<Customer>())
        {
            if (dbEntityEntry.Entity != null && dbEntityEntry.State != System.Data.Entity.EntityState.Modified && dbEntityEntry.State != System.Data.Entity.EntityState.Unchanged)
            {
                dataContext.Entry(dbEntityEntry.Entity).State = System.Data.Entity.EntityState.Unchanged;
            }
        }

Thanks to Gert for recommending using EntityState.Unchanged instead of EntityState.Detached.

like image 359
Stephen Avatar asked May 09 '17 13:05

Stephen


People also ask

How to detach object from context entity Framework?

If you want to detach an object that is already attached to the context, set the state to Detached . If you want to load entities from the DB without attaching them at all to the context (no change tracking), use AsNoTracking .

What is Entitystate detached?

An entity is in this state immediately after it has been created and before it is added to the object context. An entity is also in this state after it has been removed from the context by calling the Detach(Object) method or if it is loaded by using a NoTrackingMergeOption.


Video Answer


2 Answers

From Efcore 5.0 you can use

 _ctx.ChangeTracker.Clear();
like image 79
Thom Kiesewetter Avatar answered Oct 21 '22 13:10

Thom Kiesewetter


You could write something like

foreach (DbEntityEntry dbEntityEntry in this.ChangeTracker.Entries())
            {
                if (dbEntityEntry.Entity != null)
                {
                  // Here you can look at typeof and the EntityState
                }
            }

Thanks for KeithS for pointing out that you can also use the overload for ChangeTracker.Entries() for using specific Entity types.

this.ChangeTracker.Entries<SomeEntityEntity>()

like image 11
Mark C. Avatar answered Oct 21 '22 12:10

Mark C.