简体   繁体   English

保存或接受更改失败,因为多个类型的实体具有相同的主键值

[英]Saving or accepting changes failed because more than one entity of type have the same primary key value

I use repository pattern in asp.net mvc 5, I select the data from table and then add one entity in this data with value and I want to set this data in database with added entity value but Saving or accepting changes failed because more than one entity of type 'BlogPost.DAL.Entity.BlogPost.Blog' have the same primary key value error is occured but in database this row is inserted what I need to do avoid this error. 我在asp.net mvc 5中使用存储库模式,我从表中选择数据,然后在这个数据中添加一个实体值,我想在数据库中设置这个数据并添加实体值,但保存或接受更改失败,因为不止一个'BlogPost.DAL.Entity.BlogPost.Blog'类型的实体具有相同的主键值错误,但在数据库中插入了我需要做的行,以避免此错误。

model = await _IBlogSerices.BlogGetByID(Id);
Blog data = _db.Blog.Find(Id);
_db.Blog.Remove(data);
_db.SaveChanges();
model.IsPublish = true;
await _IBlogSerices.CreateBlog(model);
return View();

it seem there is not problem if you BlogGetByID(Id) function like 如果你BlogGetByID(Id)函数似乎没有问题

public Task<Blog> BlogGetByID(int id){
  return _db.Blog.FirstOrDefaultAsync(p => p.Id ==id);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:多个实体具有相同的主键值 - Entity Framework : more than one entity have the same primary key value 附加类型为“X”的实体失败,因为相同类型的另一个实体已具有相同的主键值 - Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value 附加类型为“”的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - Attaching an entity of type '' failed because another entity of the same type already has the same primary key value .NET - 附加类型的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - .NET - Attaching an entity of type failed because another entity of the same type already has the same primary key value 附加类型为“”的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - Attaching an entity of type “” failed because another entity of the same type already has the same primary key value “附加类型为T的实体失败,因为同一类型的另一个实体已经具有相同的主键值” - “Attaching an entity of type T failed because another entity of the same type already has the same primary key value” 附加类型为“ XXX”的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - Attaching an entity of type 'XXX' failed because another entity of the same type already has the same primary key value 附加类型的实体失败,因为相同类型的另一个实体已经具有相同的主键值 - Attaching an entity of type failed because another entity of the same type already has the same primary key value 附加类型的实体失败,因为相同类型的另一个实体已具有相同的主键值。 - Attaching an entity of type failed because another entity of the same type already has the same primary key value. 附加类型为“”的实体失败,因为另一个实体具有相同的主键值 - Attaching an entity of type '' failed because another entity has the same primary key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM