实体框架:将实体分配给另一个实体的属性
我有这些实体(这只是我为这篇文章创建的抽象):
- 语言
- 地区
- 描述
这些是它们之间的引用:
- >地区 * - 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: 无法定义关系,因为 实体集名称 'MyEntities.Descriptions' 是 对于“地区”角色无效 在关联集名称中 “MyEntities.District_Description”。
我做错了什么?
I have these entities (this is just an abstraction I created for this post):
- Language
- District
- Description
These are the references between them:
- District * - 1 Language
- Description * - 1 Language
- District 1 - 1 Description
If I fetch like this:
var myFetch = from c in context.Districts
where c.Id = 10
select new { DistrictId = c.Id, Lang = c.Language };
and after that, I try to assign it to Description like this:
Description desc = Description.CreateDescription(0, "My description");
desc.DistrictReference.EntityKey = new EntityKey("MyEntities.Descriptions", "DistrictId", myFetch.DistrictId);
desc.Language = myFetch.Lang; //throws error
The error thrown is:
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'.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如消息所说:您指定了错误的实体集名称。
Just what the message says: You specified the wrong entity set name.
如果
myFetch
是类District
的实例,您可以通过编程方式执行此操作:if
myFetch
were to be an instance of the classDistrict
you could do it programmatically: