Check if an object is being tracked in the context of an Entity Framework context

I have a block of code that checks to see if an object is being tracked in my context. If so, I need to separate it. This works for a given type T.

public virtual async Task<bool> InsertOrUpdate(TE entity)
{
    if (entity.Id == 0 || entity.Id == ModelState.New)
    {
        // attach new entity
        _context.Entry(entity).State = EntityState.Added;
    }
    else
    {
        // Sometimes when you want to update a detached entity, before attempting to attach it (by setting the .State property),
        // you first need to make sure the entity isn't already attached and being tracked. If this is the case, the existing entity
        // needs to be detached, and the updated entity, attached.
        var attachedEntity = _context.ChangeTracker.Entries<TE>().FirstOrDefault(e => e.Entity.Id == entity.Id);
        if (attachedEntity != null)
        {
            // the entity you want to update is already attached, we need to detach it and attach the updated entity instead
            _context.Entry<TE>(attachedEntity.Entity).State = EntityState.Detached;
        }

        _context.Entry<TE>(entity).State = EntityState.Modified; // Attach entity, and set State to Modified.
        _context.Entry<TE>(entity).Property(o => o.CreatedUserId).IsModified = false;
        _context.Entry<TE>(entity).Property(o => o.CreatedDate).IsModified = false;
    }

    return await _context.SaveChangesAsync() > 0;
}

Now I need to change the method so that it retrieves all objects of type IEntity within the given entity parameter T, and then performs the same logic for each object found, but I am having problems setting ChangeTracker.Entries as I need to set the type T to the current one type within foreach. I do not know how to do that.

public virtual async Task<bool> InsertOrUpdate(TE entity)
{
    //// Find all instances of IEntity within TE: 
    ////  * IF entity is new we set State to EntityState.Added (INSERT)
    ////  * IF entity is existing, we set State to EntityState.Modified (UPDATE)
    List<IEntity> found = FindAllInstances<IEntity>(entity);
    foreach (IEntity ent in found)
    {
        if (entity.Id == 0 || entity.Id == ModelState.New)
        {
            // attach new entity
            _context.Entry(entity).State = EntityState.Added;
        }
        else
        {
            // Sometimes when you want to update a detached entity, before attempting to attach it (by setting the .State property),
            // you first need to make sure the entity isn't already attached and being tracked. If this is the case, the existing entity
            // needs to be detached, and the updated entity, attached.
            var attachedEntity = _context.ChangeTracker.Entries<TE>().FirstOrDefault(e => e.Entity.Id == entity.Id);
            if (attachedEntity != null)
            {
                // the entity you want to update is already attached, we need to detach it and attach the updated entity instead
                _context.Entry<TE>(attachedEntity.Entity).State = EntityState.Detached;
            }

            _context.Entry<TE>(entity).State = EntityState.Modified; // Attach entity, and set State to Modified.
            _context.Entry<TE>(entity).Property(o => o.CreatedUserId).IsModified = false;
            _context.Entry<TE>(entity).Property(o => o.CreatedDate).IsModified = false;
        }
    }

    return await _context.SaveChangesAsync() > 0;
}
+4
source share
1 answer

You can use the internal context, which is an ObjectContext.

var ctx = ((IObjectContextAdapter)_context).ObjectContext;

ctx.Detach() , . , .

ObjetStateManager ObjectContext , .

: https://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectstatemanager(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.data.objects.objectstatemanager(v=vs.110).aspx

+3

All Articles