0

私は自分のプロジェクトにこのテンプレートを使用しています

   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)
      {
    }


}

Global.asax

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

}

しかし、エンティティを更新すると、次のエラーが発生します。

Store update、insert、または delete ステートメントが予期しない数の行 (0) に影響を与えました。エンティティが読み込まれてから、エンティティが変更または削除された可能性があります。ObjectStateManager エントリを更新する

このエラーを解決するのを手伝ってください。

4

1 に答える 1

1

スニペットで関連する行は次のとおりです。

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

次に、取得した例外を見てください。

Store update、insert、または delete ステートメントが予期しない数の行 (0) に影響を与えました。エンティティが読み込まれてから、エンティティが変更または削除された可能性があります。

  • エンティティの状態を設定してエンティティをModified 挿入しますか? いいえ。
  • エンティティを削除しますか? いいえ。
  • エンティティを更新しますか? はい。

  • EF が更新しようとしているエンティティが削除されている可能性がありますか? まあ、おそらく。それを確認する方法は?エンティティが削除されると、削除する行を知るために、データベースはキーを認識している必要があります。キーが正しいかどうかを確認するには、コントローラーのポスト アクションでデバッガーを使用categoriし、メソッドに渡されるキー値を調べます。期待値はありますか?そうでない場合は、ビューに問題があるか、フォームとルートの値をcategoriモデルにバインドすることに問題がある可能性があります。はいの場合、そのキーを持つエンティティがデータベース テーブルにあるかどうかをデータベースで確認します。はいの場合、次のポイント。

  • エンティティが変更されている可能性がありますか? Categoriモデル内の別のプロパティをコンカレンシー トークンとしてマークしている場合、EF がデータベースで変更されたと "考える" ことがあります (変更されていなくても) 。そのプロパティがデータベースまたはビューで、GET 要求でのエンティティの読み込みと再アタッチ (状態を に設定Modified) の間、およびSaveChangesPOST 要求で変更された場合、同時実行違反が発生します。

私の意見では、それが問題の原因である可能性が最も高いため、優先度には上記の太字のテストがあります。キーが期待値を持っていないことが判明した場合は、EF と UOW およびサービス アーキテクチャとは関係のない純粋な ASP.NET MVC の質問になるため、新しい質問をすることをお勧めします。

于 2013-03-28T20:46:40.670 に答える