EF错误附加类型'模型'的实体失败,因为同一类型的另一个实体已具有相同的主键值

时间:2015-05-01 13:50:09

标签: asp.net-mvc entity-framework ef-code-first entity-framework-6 repository-pattern

我将我的模型从网页传递到asp.net MVC Controller。当我尝试更新我的代码时,我采取了这个错误。我使用存储库模式。我在网站上读取相同的错误,但我不知道如何适应我的代码。

错误:附加类型'模型'的实体失败,因为同一类型的另一个实体已具有相同的主键值。

 public void Update(Model model)
            {
                model.AnaHesapID = Thread.CurrentPrincipal.Identity.GetAnaHesapID();
                if (model.RowNumber != Guid.Empty && model.ModelID== 0)
                {
    //when i comment this i take 0 record updated error.
                    var oldModel= Get(calisan.RowNumber);
                    model.ModelID = oldModel.ModelID;
                }
                _myDal.Update(model);
            }

    //Repository Code
        public virtual int Update(TEntity TObject)
            {

                //ADD LAST MODIFIED DATE IF APPLICABLE
                if (TObject is LogProperties)
                {
                    (TObject as LogProperties).DegistirmeTarihi = DateTime.UtcNow;
                }
                dbContext.Set<TEntity>().Attach(TObject);
                var entry = dbContext.Entry(TObject);
                entry.State = EntityState.Modified;
                return dbContext.SaveChanges();
            }
    //Get Context
      private TContext dbContext;
            private int _anaHesapID;
            public EfEntityRepositoryBase()
            {
                _anaHesapID = Thread.CurrentPrincipal.Identity.GetAnaHesapID();
                dbContext = GetDbContext();

            }
            #region GetContext
            //Update de patlıyor link: http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent
            private TContext GetDbContext()
            {
                return this.GetDbContext(false);
            }


            protected virtual TContext GetDbContext(bool canUseCachedContext)
            {
                if (dbContext != null)
                {
                    if (canUseCachedContext)
                    {
                        return dbContext;
                    }
                    else
                    {
                        dbContext.Dispose();
                    }
                }

                dbContext = new TContext();

                return dbContext;
            }
            #endregion
    //Dispose
     private bool disposed = false;

            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        dbContext.Dispose();
                    }
                }
                this.disposed = true;
            }

            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }

我将我的代码更改为此及其工作。但我不确定正确的方法。

private TContext _dbContext;
  public TContext dbContext
        {
            get { return GetDbContext(); }

        }

 protected virtual TContext GetDbContext(bool canUseCachedContext)
        {
            if (_dbContext != null)
            {
                if (canUseCachedContext)
                {
                    return _dbContext;
                }
                else
                {
                    _dbContext.Dispose();
                }
            }

            _dbContext = new TContext();

            return _dbContext;
        }
//Update Function
 public virtual int Update(TEntity TObject)
        {


            var contextRef= dbContext;
            contextRef.Set<TEntity>().Attach(TObject);
            var entry = contextRef.Entry(TObject);
            entry.State = EntityState.Modified;
            return contextRef.SaveChanges();
        }

0 个答案:

没有答案