检查实体框架上下文是否正在跟踪实体

FaN*_*NIX 5 c# generics entity-framework-6

我有一个代码块,用于检查我的上下文是否正在跟踪实体.如果是的话,我需要分开它.这适用于给定的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;
}
Run Code Online (Sandbox Code Playgroud)

我现在需要更改方法,以便在给定的T实体参数中获取IEntity类型的所有对象,然后为找到的每个对象执行相同的逻辑,但是我在设置ChangeTracker.Entries时遇到问题,因为我需要设置将T类型转换为foreach中当前选定的类型.我不知道该怎么做.

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;
}
Run Code Online (Sandbox Code Playgroud)

Geo*_*ica 3

您可以使用内部上下文,即 ObjectContext。

var ctx = ((IObjectContextAdapter)_context).ObjectContext;
Run Code Online (Sandbox Code Playgroud)

然后调用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