[英]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.