Attaching an entity of type 'EntityModel.Models.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if
any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state
of non-new entities to 'Unchanged' or 'Modified' as appropriate.
my code:
public abstract class RepositoryBase<T> : IRepository<T> where T : class
{
#region Properties
private AADbContext dataContext;
private readonly IDbSet<T> dbSet;
protected IdbAA dbAA
{
get; private set;
}
protected AADbContext DbContext
{
get { return dataContext ?? (dataContext = dbAA.Init()); }
As we can see in the code, you are trying to attach the entity before updating. It will set the entity's state to unchanged. It will conflict with updating. Please remove this code form the update method.
public void Update(T NewEntity)
{
if (ModelState.IsValid)
{
dataContext.Entry(NewEntity).State = EntityState.Modified;
dataContext.SaveChanges();
}
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
26 Points
98 Posts
Curd opration in mvc
Nov 30, 2017 01:12 PM|sanjaykumar pushadapu|LINK
Attaching an entity of type 'EntityModel.Models.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
my code:
public abstract class RepositoryBase<T> : IRepository<T> where T : class
{
#region Properties
private AADbContext dataContext;
private readonly IDbSet<T> dbSet;
protected IdbAA dbAA
{
get; private set;
}
protected AADbContext DbContext
{
get { return dataContext ?? (dataContext = dbAA.Init()); }
}
#endregion
protected RepositoryBase(IdbAA dbAA)
{
dbAA = dbAA;
dbSet = DbContext.Set<T>();
}
#region IRepository Members
public void Update(T entity, T NewEntity)
{
this.dbSet.Attach(entity);
dataContext.Entry(entity).CurrentValues.SetValues(NewEntity);
dataContext.Entry(entity).State = EntityState.Modified;
dataContext.SaveChanges();
}
public virtual void Delete(T entity)
{
this.dbSet.Remove(entity);
dataContext.SaveChanges();
}
#endregion
}
Contributor
2400 Points
730 Posts
Re: Curd opration in mvc
Dec 01, 2017 07:20 AM|X.Daisy|LINK
Hi sanjaykumar pushadapu,
As we can see in the code, you are trying to attach the entity before updating. It will set the entity's state to unchanged. It will conflict with updating. Please remove this code form the update method.
For more details, please refer to this article.
Best Regards,
Daisy
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.