locked
EF Error Attaching an entity of type 'Model' failed because RRS feed

  • Question

  • I pass my model from webpage to asp.net MVC Controller.When i try to update my code i take this error.I use repository pattern.I read same errors on site but i cant figure how can adapt to my code.

    Error: Attaching an entity of type 'Model' failed because another entity of the same type already has the same primary key value.

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

    Friday, May 1, 2015 7:36 PM

Answers

  • Hello Cra2385,

    Entity Framework would maintain a context object which only could contain an entity with a unique key value. It you are trying to add a same one, it will throw the exception you encounter. In your case, you could check if the entity you want to attach already exists in the context as query it Local property: https://msdn.microsoft.com/en-us/data/jj592872.aspx?f=255&MSPPError=-2147217396 in your update method, if exists, detecting the original one and then attaching the new one.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Tuesday, May 12, 2015 8:20 AM
    Monday, May 4, 2015 8:10 AM

All replies