Entity framework 实体框架:将实体分配给另一个实体的属性
我有这些实体(这只是我为这篇文章创建的抽象):Entity framework 实体框架:将实体分配给另一个实体的属性,entity-framework,entity-relationship,variable-assignment,entityreference,Entity Framework,Entity Relationship,Variable Assignment,Entityreference,我有这些实体(这只是我为这篇文章创建的抽象): 语言 地区 说明 以下是它们之间的参考: 地区*-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 = Descriptio
- 语言
- 地区
- 说明
- 地区*-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
然后,我尝试将其分配到描述中,如下所示:
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:异常
无法定义关系,因为
EntitySet名称
“MyEntities.Descriptions”是
对于角色“District”无效
关联集合名称中的
“MyEntities.地区描述”
我做错了什么?正如消息所说:您指定了错误的实体集名称
如果
myFetch
是类District
的一个实例,则可以通过编程实现:
desc.DistrictReference.EntityKey = new EntityKey(
String.Format(
"{0}.{1}",
myFetch.EntityKey.EntityContainerName,
myFetch.EntityKey.EntitySetName),
"DistrictId",
myFetch.DistrictId);
不,事实并非如此。不管怎样,District不是在字符串中使用的,所以我不可能拼错它;你只是还没找到而已。所有EF名称最终返回字符串(在EDMX中)。就像我说的,看看地图。描述可能是您想要的EntitySet名称,但它不是模型中的名称。Argh。。。我把EntityKey放错一边了。。。。它应该是:desc.DistrictReference.EntityKey=newEntityKey(“MyEntities.Districts”,“Id”,myFetch.DistrictId);谢谢我喜欢这个-它省去了拼写错误的麻烦