2016-11-08 55 views
1

我在asp.net mvc 5中使用存储库模式,我从表中选择数据,然后在数据中添加一个实体,其值和I想要在具有添加实体值的数据库中设置此数据,但保存或接受更改失败,因为类型为“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(); 
+0

请添加代码作为文本,而不是作为一个图像 – Rob

+0

你在做什么,删除博客和创建一个具有相同ID的博客。你想达到什么目的? – Marc

回答

0

它似乎没有问题,如果你BlogGetByID(Id)的功能就像

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