Entity object cannot reference multiple instances of IEntityChangeTracker

I am using EF as ORM.

I use objectContext

for every request.

I am storing objects at the cache level since my service is receiving a lot of traffic.

I sometimes get an error objectContext already disposed

for some objects that I got from the cache.

I added this code to the items that were fetched from the cache

if (maMDBEntities.Entry(group).State == EntityState.Detached)
{
    maMDBEntities.Groups.Attach(group);
}

      

but now I sometimes get this error:

An entity object cannot reference multiple instances of IEntityChangeTracker.

Attach()

Did you use the wrong solution from the first place?

+3


source to share


1 answer


As we told you in your other question ( will EF :: attach (entity) solve the objectContext problem? ), You need to detach the objects before attaching it to another Context!

If maMDBEntities

is a new Context (not the one that is loading the data), the EntityState is not tied to that context point. Therefore, your check is not enough.



This maMDBEntities.Entry(group).State == EntityState.Detached

will always be true for the Context that did not load the Entity.

+4


source







All Articles