简体   繁体   English

EntityFramework上下文未保存

[英]EntityFramework Context is not saved

I have some actions to my DbContext for save. 我对DbContext进行了一些保存操作。

I have two Entities - Material and Colors. 我有两个实体-材质和颜色。 The entities have relationship many to many. 实体之间的关系是多对多的。

I try to do this: 我尝试这样做:

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

When program try to run context.SaveChanges() , it throws an exception DbUpdateException 当程序尝试运行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.

How to solve it? 怎么解决呢?

Every table mapped by EF must have PK. EF映射的每个表都必须具有PK。 If it doesn't have PK it will be mapped as readonly view. 如果没有PK,它将被映射为只读视图。 You must go to your database and in MaterialColors mark both FKs as composite PK. 您必须转到数据库,然后在MaterialColors中将两个FK都标记为复合PK。 Than update your model. 比更新您的模型。

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

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