我有这些实体(这只是我为这篇文章创建的抽象):
- 语言
- 地区
- 描述
这些是它们之间的引用:
- 地区 * - 1 语言
- 描述 * - 1 语言
- 地区 1 - 1 描述
如果我像这样获取:
var myFetch = from c in context.Districts
where c.Id = 10
select new { DistrictId = c.Id, Lang = c.Language };
之后,我尝试将其分配给描述,如下所示:
Description desc = Description.CreateDescription(0, "My description");
desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Descriptions", "DistrictId", myFetch.DistrictId);
desc.Language = myFetch.Lang; //throws error
抛出的错误是:
System.InvalidOperationException: The relationship cannot be defined because the EntitySet name 'MyEntities.Descriptions' is not valid for the role 'District' in association set name 'MyEntities.District_Description'.
我做错了什么?
最佳答案
正如消息所说:您指定了错误的实体集名称。
- 打开您的 EDMX。
- 打开模型浏览器窗口。
- 在模型浏览器中查找 District 实体
- 右键单击它,选择“属性”
- 记下正确的实体集名称
关于entity-framework - Entity Framework : Assign entity to property of another entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1498405/