检查实体是否被实体框架上下文跟踪
本文关键字:实体 上下文 跟踪 框架 是否 检查 | 更新日期: 2023-09-27 18:01:39
我有一个代码块,它检查实体是否被我的上下文跟踪。如果是,我得把它拆下来。这适用于给定的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;
}
我现在需要更改方法,以便它在给定的T实体参数中获取IEntity类型的所有对象,然后对找到的每个对象执行相同的逻辑,但是我在设置ChangeTracker时遇到了麻烦。条目,因为我需要将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;
}
可以使用ObjectContext作为内部上下文。
var ctx = ((IObjectContextAdapter)_context).ObjectContext;
然后在任意实体上调用ctx.Detach()
。幸运的是,这不是一个泛型方法。
您还可以从ObjectContext
中获得对ObjetStateManager
的引用,并使用它来执行您想要的任何状态更改。
更多信息: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