C# EntityState.Modified错误“;相同类型的实体已具有相同的主键值;
我在运行Edit[Post]方法时出现以下错误: 附加“MyType.Models.ApplicationUser”类型的实体失败,因为相同类型的另一个实体已具有相同的主键值。如果图形中的任何实体具有冲突的键值,则在使用“Attach”方法或将实体状态设置为“Unchanged”或“Modified”时可能会发生这种情况。这可能是因为某些实体是新的,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“添加”实体状态跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改” 我创建了ApplicationUser类(Identity framework)的一个新实例,其中包含所有必需字段,并将其保存到现有用户的数据库中,作为对用户“编辑”[Post]操作的响应,“db”是MyDbContext类型的私有变量。为了可读性,我简化了代码 这是我的控制器:C# EntityState.Modified错误“;相同类型的实体已具有相同的主键值;,c#,asp.net,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Identity,我在运行Edit[Post]方法时出现以下错误: 附加“MyType.Models.ApplicationUser”类型的实体失败,因为相同类型的另一个实体已具有相同的主键值。如果图形中的任何实体具有冲突的键值,则在使用“Attach”方法或将实体状态设置为“Unchanged”或“Modified”时可能会发生这种情况。这可能是因为某些实体是新的,尚未收到数据库生成的键值。在这种情况下,使用“添加”方法或“添加”实体状态跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改” 我创建
public ActionResult Edit([Bind(Include = "Id,a,b,c,d,e")] ModelClass editParam)
{
ApplicationUser user = new ApplicationUser()
{
Id = editParam.Id,
a= editParam.a,
b = editParam.b,
c= editParam.c,
d= editParam.d,
e= editParam.e,
};
string[] userRoles = editParam.Roles.Split(new[] { ", " }, StringSplitOptions.None);
var userStore = new UserStore<ApplicationUser>(db);
var userManager = new UserManager<ApplicationUser>(userStore);
userManager.RemoveFromRoles(user.Id, GetAllUserRoles());
userManager.AddToRoles(user.Id, userRoles);
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UsersList");
}
你能告诉我当你尝试这个时会发生什么吗
//I named my dbcontext, yours has a different class name
//My class name, as an example, is called NoId, yours is different
using (BreazEntities29 entity = new BreazEntities29())
{
var id = entity.NoIds.Where(r => r.Id == 1).ToList();
//if you are calling into a method, where you can not have control of AsNoTracking(),
//then you can put the foreach statement shown here after calls into db change methods
//credit to https://stackoverflow.com/questions/27423059/how-do-i-clear-tracked-entities-in-entity-framework
//foreach (var db in entity.ChangeTracker.Entries())
//{
// db.State = EntityState.Detached;
//}
NoId noid = new NoId { Id = 1, NoId1 = "new" };
entity.Entry(noid).State = EntityState.Modified;
}
return RedirectToAction("UsersList");
你能告诉我当你尝试这个时会发生什么吗
//I named my dbcontext, yours has a different class name
//My class name, as an example, is called NoId, yours is different
using (BreazEntities29 entity = new BreazEntities29())
{
var id = entity.NoIds.Where(r => r.Id == 1).ToList();
//if you are calling into a method, where you can not have control of AsNoTracking(),
//then you can put the foreach statement shown here after calls into db change methods
//credit to https://stackoverflow.com/questions/27423059/how-do-i-clear-tracked-entities-in-entity-framework
//foreach (var db in entity.ChangeTracker.Entries())
//{
// db.State = EntityState.Detached;
//}
NoId noid = new NoId { Id = 1, NoId1 = "new" };
entity.Entry(noid).State = EntityState.Modified;
}
return RedirectToAction("UsersList");
尝试检索现有用户并更新它。您需要在linq中为您的用户实体放置一个AsNoTracking(),第一次调用它时。@Simon,这意味着要更改大部分代码,我在使用Identity framework提供的ApplicationUser类而不是现在使用的自定义模型类列出用户角色时遇到问题。var DbQuery=db.Users.AsNoTracking().SingleOrDefault(user=>user.Id==Id);“但是,如果您没有两次从数据库获取用户,那么还有另一个问题。@kblau,感谢您回答有关使用“.AsNoTracking()”方法的问题。我有一个类,其中多次使用同一个db上下文变量,是否需要设置“.AsNoTracking()”'everywhere或我可以使用某种方法来防止两次获取同一用户,我认为这里就是这种情况吗?我在编辑[Post]方法之前的最后一次查询中将其设置为db,错误仍然存在/相同。谢谢。尝试检索现有用户并进行更新。您需要放置AsNoTracking()在linq中,第一次调用用户实体时。@Simon,这意味着要更改大部分代码,我在使用Identity framework提供的ApplicationUser类而不是现在使用的自定义模型类列出用户角色时遇到问题。var DbQuery=db.Users.AsNoTracking().SingleOrDefault(user=>user.Id==Id)但是如果您没有两次从数据库获取用户,那么还有另一个问题。@kblau,感谢您回答有关使用“.AsNoTracking()”方法的问题。我有一个类,同一个db上下文变量被多次使用,我需要在任何地方设置“.AsNoTracking()”吗?或者我可以使用一些东西来防止同一个用户被使用两次,我想这里就是这样的情况?我在编辑[Post]方法之前的最后一次查询中将其设置为db,错误仍然存在/相同。谢谢。嘿,我得到了完全相同的错误,正如第'db.Entry(user.State=EntityState.Modified'P.S.'scope'变量调试'中所述嘿,我得到了完全相同的错误,正如第'db.Entry(user.State=EntityState.Modified'P.S.'scope'变量调试'中所述