当我尝试通过EF 6删除某个对象时,发生错误无法删除该对象,因为在ObjectStateManager中找不到该对象。
但是你可以在图像中看到,TEntity obj正确地接收了一个对象。
代码
public class RepositoryBase<TEntity, TContext> : IRepositoryBase<TEntity>
where TEntity : class
where TContext : IDbContext, new()
{
private readonly ContextManager<TContext> _contextManager = ServiceLocator.Current.GetInstance<IContextManager<TContext>>() as ContextManager<TContext>;
protected IDbSet<TEntity> DbSet;
protected readonly IDbContext Context;
public RepositoryBase()
{
Context = _contextManager.GetContext();
DbSet = Context.Set<TEntity>();
}
public virtual void Add(TEntity obj)
{
DbSet.Add(obj);
}
public virtual TEntity GetById(Guid id)
{
return DbSet.Find(id);
}
public virtual IEnumerable<TEntity> GetAll()
{
return DbSet.ToList();
}
public virtual IEnumerable<TEntity> GetAllReadOnly()
{
return DbSet.AsNoTracking();
}
public virtual void Update(TEntity obj)
{
var entry = Context.Entry(obj);
DbSet.Attach(obj);
entry.State = EntityState.Modified;
}
public virtual void Remove(TEntity obj)
{
DbSet.Remove(obj);
}
public virtual IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return DbSet.Where(predicate);
}
public void Dispose()
{
Context.Dispose();
GC.SuppressFinalize(this);
}
}