我有这个代码,但我得到了异常
ObjectStateManager 中已存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象。
在其他部分。
public int AddOrUpdateEntity<T>(T entity) where T : class , IEntity
{
int numberOfobjectsWritten = 0;
using (DalContext dbContext = new DalContext())
{
//If Id == 0 it means it's a new entity in Db and needs to be added
dbContext.Entry<T>(entity).State = entity.Id == 0 ?
EntityState.Added :
EntityState.Modified;
numberOfobjectsWritten = dbContext.SaveChanges();
}
}