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

时间:2016-11-08 08:14:08

标签: c#

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

1 个答案:

答案 0 :(得分:0)

如果您使用BlogGetByID(Id)函数

,似乎没有问题
public Task<Blog> BlogGetByID(int id){
  return _db.Blog.FirstOrDefaultAsync(p => p.Id ==id);
}