实体框架更新无跟踪实体

Aza*_*rsa 4 asp.net-mvc entity-framework repository-pattern

如何更新与上下文分离的实体AsNoTracking()

var _agency = agencyRepository.Get(filter: a => a.Id == agency.Id)
                              .AsQueryable()
                              .AsNoTracking()
                              .FirstOrDefault(); 
agencyRepository.Update(_agency);
Run Code Online (Sandbox Code Playgroud)

并且我的 Update 方法已经设置修改:

public virtual void Update(T entity)
    {            
        dbset.Attach(entity);
        dataContext.Entry(entity).State = System.Data.Entity.EntityState.Modified;
    }
Run Code Online (Sandbox Code Playgroud)

我可以找到数据上下文附加的前一个实体吗?或者有什么建议可以防止跟踪我的用户实体?

Moh*_*deh 6

您可以更改实体的状态:

ctx.Entry(_agency).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
Run Code Online (Sandbox Code Playgroud)

更多阅读这篇这篇文章。