【问题标题】:Edit entity in Ef - UnitOfWork在 Ef 中编辑实体 - UnitOfWork
【发布时间】:2013-03-28 12:19:49
【问题描述】:

我正在为我的项目使用这个模板

   public interface IUnitOfWork
    {
        IDbSet<TEntity> Set<TEntity>() where TEntity : class;
        int SaveChanges();
        void RejectChanges();
        DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
    }

实施:

  public   class BookStoreDbContext : DbContext, IUnitOfWork

  {

    public DbSet<Categori> Categoris { get; set; }


    public new DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class
    {
        return base.Entry(entity);

    }

    public override int SaveChanges()
    {

        return base.SaveChanges();
    }

控制器:

 public class CategoriController : Controller
   {

    private IUnitOfWork _uw;

    private ICategoriService _categoriService;


    public CategoriController(IUnitOfWork uw,ICategoriService categoriservice )
    {
        _uw = uw;
        _categoriService = categoriservice;
    }




   public ActionResult Edit(int id = 0)
    {
        var categori = _categoriService.Find(i => i.Id == id);
        if (categori == null)
        {
            return HttpNotFound();
        }
        return View(categori);
    }

    [HttpPost]
    public ActionResult Edit(Categori categori)
    {
        if (ModelState.IsValid)
        {

              _uw.Entry(categori).State = EntityState.Modified;
                _uw.SaveChanges();
        }
        return View(categori);
    }
}

存储库或服务层:

 public interface IGenericService<T> : IDisposable where T : class
    {
    void Add(T entity);

    void Delete(T entity);
    T Find(Func<T, bool> predicate);
    IList<T> GetAll();
    IList<T> GetAll(Func<T, bool> predicate);


    }




 public interface ICategoriService : IGenericService<DomainClasses.Models.Categori>
   {

   }

实现存储库:

 public class EfGenericService<TEntity> : IGenericService<TEntity> where TEntity : class
{
    protected IUnitOfWork _uow;
    protected IDbSet<TEntity> _tEntities;



    public EfGenericService(IUnitOfWork uow)
    {
        _uow = uow;
        _tEntities = _uow.Set<TEntity>();
    }


    public virtual void Add(TEntity entity)
    {
        _tEntities.Add(entity);
    }

    public void Delete(TEntity entity)
    {
        _tEntities.Remove(entity);
    }

    public TEntity Find(Func<TEntity, bool> predicate)
    {
        return _tEntities.Where(predicate).FirstOrDefault();
    }

    public IList<TEntity> GetAll()
    {
        return _tEntities.ToList();
    }

    public IList<TEntity> GetAll(Func<TEntity, bool> predicate)
    {
        return _tEntities.Where(predicate).ToList();
    }



 public class EfCategoriService : EfGenericService<Categori>,ICategoriService
{
      public EfCategoriService(IUnitOfWork uow)
        : base(uow)
      {
    }


}

全球.asax

 private static void InitStructureMap()
    {
        ObjectFactory.Initialize(
            x =>
                {
                    x.For<IUnitOfWork>().HttpContextScoped().Use(() => new BookStoreDbContext());
                     x.ForRequestedType<ServiceLayer.Interfaces.ICategoriService>()
                     .TheDefaultIsConcreteType<EfCategoriService>();

}

但更新实体时出现此错误:

存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目

请帮我解决这个错误?

【问题讨论】:

  • 您正在使用某种类型的依赖注入?您能否展示一下您是如何为 DI 配置 IUnitOfWork 和 ICategoriService 的?
  • 是的,这已经完成了我已经更新了代码和依赖

标签: asp.net-mvc entity-framework c#-4.0 unit-of-work


【解决方案1】:

您的 sn-ps 中唯一相关的行是:

_uw.Entry(categori).State = EntityState.Modified;
_uw.SaveChanges();

现在,看看你得到的异常:

存储更新、插入或删除语句影响了意外 行数 (0)。实体可能已被修改或删除 实体已加载。

  • 是否将实体状态设置为Modified插入一个实体?没有。
  • 它是否删除一个实体?没有。
  • 它是否更新一个实体?是的。

  • EF 尝试更新的实体是否已被删除?好吧,也许。如何检查?删除实体时,数据库必须知道键才能知道要删除哪一行。要确认密钥是否正确在控制器发布操作中使用调试器,请检查传递给方法的 categori 的密钥值。它有预期值吗?如果不是,您的视图或将表单和路由值绑定到categori 模型时可能有问题。 如果是,请检查数据库中具有该键的实体是否在数据库表中。如果是,下一点。

  • 实体可能已被修改?如果您已将 Categori 模型中的另一个属性标记为并发令牌,则 EF 可能会“认为”它已在数据库中被修改(即使它没有)。如果在 GET 请求中加载实体和重新附加(将状态设置为Modified)和 POST 请求中的SaveChanges 之间的数据库或视图中该属性已更改,您将获得并发冲突。

优先级在上面以粗体显示测试,因为在我看来它是问题的最可能原因。如果结果证明密钥没有预期值,最好提出一个新问题,因为这将是一个纯 ASP.NET MVC 问题,与 EF 以及您的 UOW 和服务架构无关。

【讨论】:

  • 感谢您的回答。在上面的示例中,我正在删除和添加。但是我在编辑时遇到了这个错误。我什至 Get 方法是编辑它。我把它放在上面的代码中。
  • @MehRad:我不确定你是否理解我的意思。重要的是要检查 categori Id 在进入 Edit POST 操作 public ActionResult Edit(Categori categori) 时具有什么值。你必须检查这个,我无法从你显示的代码中知道它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
相关资源
最近更新 更多