为什么我收到错误“无法设置类型 Y 上的属性 X,因为集合已设置为 EntityCollection”?
当我尝试将一个集合映射到 EF4 中的另一个集合时,出现此错误。
无法设置类型“Resource_EF810770B4FCA2E071F38C2F2EE328AAC216CA2A7BF157503E6658A42D7CF53A”的属性“ResourceLanguages”,因为该集合已设置为 EntityCollection。
我正在尝试这样编码
foreach (var resource in resources)
{
resourceLanguages = resourceLanguageRepositoty.GetAllByResourceId(resource.Id);
resource.ResourceLanguages = resourceLanguages;
}
有人能帮我解决这个问题吗?
While I was trying to map a collection to another in EF4 I got this error.
The property 'ResourceLanguages' on type 'Resource_EF810770B4FCA2E071F38C2F2EE328AAC216CA2A7BF157503E6658A42D7CF53A' cannot be set because the collection is already set to an EntityCollection.
I was trying to code like this
foreach (var resource in resources)
{
resourceLanguages = resourceLanguageRepositoty.GetAllByResourceId(resource.Id);
resource.ResourceLanguages = resourceLanguages;
}
Can anyone help me to sort this out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用代理时,无法将集合分配给具体化导航属性。您找到了一种解决方案,但恕我直言,它看起来非常无效。首先,如果您的资源附加到上下文,一旦需要,语言将通过延迟加载进行加载,但您也可以使用预先加载并在单个查询中加载所有资源及其语言:
您的解决方案会产生 N+1 数据库查询,其中 N是集合中资源的数量。
You can't assign collection to materialized navigation property when using proxies. You find one solution but imho it looks quite ineffective. First if your resources are attached to context, languages will be loaded by lazy loading once they are needed but you can also use eager loading and load all resources with their languages in a single query:
Your solution results in N+1 database queries where N is number of resources in the collection.