【发布时间】:2012-05-09 22:53:46
【问题描述】:
我对我的 DbContext 进行了一些操作以进行保存。
我有两个实体 - 材质和颜色。实体具有多对多的关系。
我尝试这样做:
var color = context
.Colors
.Include("Materials")
.Where(g => g.Id == (colorId))
.FirstOrDefault();
var mater = context
.Materials
.Include("Colors")
.SingleOrDefault(c => c.Name == material.Name);
mater.Colors.Add(color);
context.SaveChanges();
当程序尝试运行context.SaveChanges()时,会抛出异常DbUpdateException
Unable to update the EntitySet 'MaterialColors' because it has a
DefiningQuery and no <InsertFunction> element exists in
the <ModificationFunctionMapping> element to support the current operation.
如何解决?
【问题讨论】:
-
数据库是Sql 2008 express。模型优先
-
这是模型中的一个奇怪问题。您的 MaterialColors 表看起来如何?是不是只有两个FK做PK?
-
MaterialsColors 表是由模型创建的,我在其中设置了 Materials 和 Colors 多对多。此表未PK
-
所以我猜该表只有两个 FK。尝试从模型中删除关系并从数据库中更新它。您的模型存在问题,因为它试图使用存储的过程在该表中插入值。您使用的是 EF 4.0 还是其他版本?
-
我忘记了:在测试之前备份你的模型文件。
标签: c# .net entity-framework exception dbcontext