繁体   English   中英

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

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

我在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();

如果你BlogGetByID(Id)函数似乎没有问题

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM