在向表中插入数据时出现错误,“无法更新实体集”。

24 浏览
0 Comments

在向表中插入数据时出现错误,“无法更新实体集”。

我正在尝试将数据保存到数据库中。表名为product_color

public void PostAddColour1()
{
     product_color pc = new product_color();
     pc.id = 999;
     pc.product_id=2;
     pc.color_id=1;
     pc.display_order=3;
     db.product_color.Add(pc);
     db.SaveChanges();
}

这是将数据插入表中的函数代码。

当调用db.SaveChanges();时,下面的错误会显示:

Unable to update the EntitySet 'product_color' because it has a
DefiningQuery and no  element exists in the 
ModificationFunctionMapping> element to support the current operation.

admin 更改状态以发布 2023年5月21日
0
0 Comments

当您的表没有定义任何主键时,通常会出现这种情况。如果一个表没有主键,Entity Framework 视其为视图而不是,因此它不允许直接向其插入数据。

只需在表上设置一个主键就可以解决这个问题。

您可以在这里了解更多信息。

0