EF多2对映射方案 [英] EF many-2-many mapping scenario

查看:64
本文介绍了EF多2对映射方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有3个表:


1)帐户(PK(AccountId),电子邮件等)。)


2)标签(PK(TagId) ,价值)


3)Account2Tag(PK(AccountId,TagId,Kind)) - 其中种类意味着标签类型:1 - 技能,2 - 经验


 


我想创建一个类似

的域模型

类帐户{


int AccountId {...},


字符串电子邮件{...},


...


EntityCollection<标签>技能{...},


EntityCollection< Tag>体验{...}


}


 


我试图在概念上定义2个AssociationSet model:


< AssociationSet Name =" AccountExperience" Association =" Model1.AccountExperience">


< End Role =" Account" EntitySet的= QUOT;帐户" />


< End Role =" Tag" EntitySet的= QUOT;标签" />


< / AssociationSet>


< AssociationSet Name =" AccountSkills" Association =" Model1.AccountSkills">


< End Role =" Account" EntitySet的= QUOT;帐户" />


< End Role =" Tag" EntitySet的= QUOT;标签" />


< / AssociationSet>


 


... + 2 nav。帐户的EntityType中的属性:


< NavigationProperty Name =" Experience"关系= QUOT; Model1.AccountExperience" FromRole = QUOT;帐户] ToRole = QUOT;标签" />


< NavigationProperty Name =" Skills"关系= QUOT; Model1.AccountSkills" FromRole = QUOT;帐户] ToRole = QUOT;标签" />


 


... + 2关联


<关联名称=" ; AccountExperience">


< End Type =" Model1.Account"角色= QUOT;帐户]多重= QUOT * QUOT; />


< End Type =" Model1.Tag"角色= QUOT;标签"多重= QUOT * QUOT; />


< / Association> < Association Name =" AccountSkills">


< End Type =" Model1.Account"角色= QUOT;帐户]多重= QUOT * QUOT; />


< End Type =" Model1.Tag"角色= QUOT;标签"多重= QUOT * QUOT; />


< / Association>


 


... + MDL中的AssociationSetMapping具有不同条件


< AssociationSetMapping Name =" AccountExperience"类型名= QUOT; Model1.AccountExperience" StoreEntitySet =" Account2Tag">


< EndProperty Name =" Tag">< ScalarProperty Name =" TagId"的ColumnName = QUOT; TAGID" />< / EndProperty>


< EndProperty Name =" Account">< ScalarProperty Name =" AccountId"的ColumnName = QUOT; ACCOUNTID" />< / EndProperty>


<条件ColumnName =" iKind"值= QUOT 1 QUOT; />


< / AssociationSetMapping>


< AssociationSetMapping Name =" AccountSkills"类型名= QUOT; Model1.AccountSkills" StoreEntitySet =" Account2Tag">


< EndProperty Name =" Tag">< ScalarProperty Name =" TagId"的ColumnName = QUOT; TAGID" />< / EndProperty>


< EndProperty Name =" Account">< ScalarProperty Name =" AccountId"的ColumnName = QUOT; ACCOUNTID" />< / EndProperty>


<条件ColumnName =" iKind"值= QUOT; 2英寸/>


< / AssociationSetMapping>


 


但我收到错误:错误3034:从第175,184行开始映射片段的问题:具有可能相同键的两个实体被映射到同一表内的不同行。确保这两个映射片段不会将两个不相关的EntitySet映射到两个不同的行组。


请解释一下这是什么意思以及我如何实现我的场景?谢谢。


 


Alimov Sergey


 


PS我理解,最好的解决方案是将Account2Tag拆分为2个表,但这是为了更好地理解EEF的可能性。

解决方案

不幸的是,在AssociationSetMappings上不允许指定的条件。有很多方法可以解决这个问题。我要采取的一种方法是使Account2Tag表成为一流的实体,其中包含与帐户和标签的1:*关联。您可以根据导航属性设置查询过滤器,也可以创建SkillTag的简单层次结构:Account2Tag和ExperienceTag:Account2Tag。对于基本类型Account2Tag,您可以提供"虚假"信息。暂时的条件,或者,如果手动编写XML,您可以将其定义为抽象而不是映射它 - 而是仅为SkillTag和ExperienceTag提供EntitySet。


HTH,
&NBSP;诺姆


Hi, all! I have 3 tables:

1) Account (PK(AccountId), Email, etc..)

2) Tag (PK(TagId), Value)

3) Account2Tag (PK(AccountId, TagId, Kind)) - where the Kind means tag type: 1 - Skills, 2 - Experience

 

I want to make a domain model like

class Account {

int AccountId { ... },

string Email { ... },

...

EntityCollection<Tag> Skills { ... },

EntityCollection<Tag> Experience { ... }

}

 

I tried to define 2 AssociationSet in conceptual model:

<AssociationSet Name="AccountExperience" Association="Model1.AccountExperience">

<End Role="Account" EntitySet="Accounts" />

<End Role="Tag" EntitySet="Tags" />

</AssociationSet>

<AssociationSet Name="AccountSkills" Association="Model1.AccountSkills">

<End Role="Account" EntitySet="Accounts" />

<End Role="Tag" EntitySet="Tags" />

</AssociationSet>

 

... + 2 nav. properties in the EntityType for Account:

<NavigationProperty Name="Experience" Relationship="Model1.AccountExperience" FromRole="Account" ToRole="Tag" />

<NavigationProperty Name="Skills" Relationship="Model1.AccountSkills" FromRole="Account" ToRole="Tag" />

 

... + 2 association

<Association Name="AccountExperience">

<End Type="Model1.Account" Role="Account" Multiplicity="*" />

<End Type="Model1.Tag" Role="Tag" Multiplicity="*" />

</Association> <Association Name="AccountSkills">

<End Type="Model1.Account" Role="Account" Multiplicity="*" />

<End Type="Model1.Tag" Role="Tag" Multiplicity="*" />

</Association>

 

... + AssociationSetMapping in MDL with different conditions

<AssociationSetMapping Name="AccountExperience" TypeName="Model1.AccountExperience" StoreEntitySet="Account2Tag">

<EndProperty Name="Tag"><ScalarProperty Name="TagId" ColumnName="TagId" /></EndProperty>

<EndProperty Name="Account"><ScalarProperty Name="AccountId" ColumnName="AccountId" /></EndProperty>

<Condition ColumnName="iKind" Value="1" />

</AssociationSetMapping>

<AssociationSetMapping Name="AccountSkills" TypeName="Model1.AccountSkills" StoreEntitySet="Account2Tag">

<EndProperty Name="Tag"><ScalarProperty Name="TagId" ColumnName="TagId" /></EndProperty>

<EndProperty Name="Account"><ScalarProperty Name="AccountId" ColumnName="AccountId" /></EndProperty>

<Condition ColumnName="iKind" Value="2" />

</AssociationSetMapping>

 

But i take an error: Error 3034: Problem in mapping fragments starting at lines 175, 184: Two entities with possibly identical keys are mapped to different rows within the same table. Ensure these two mapping fragments do not map two unrelated EntitySets to two distinct groups of rows.

Please, explain me what does it mean and how i can realize my scenario? Thank you.

 

Alimov Sergey

 

P.S. I understand, that the best solution is splitting Account2Tag to 2 tables, but it is for better understanding of EEF possibilities.

解决方案

Unfortunately, the conditions you are specifying are not allowed on AssociationSetMappings. There are a number of ways around this. The one I would take is to make the Account2Tag table a first-class entity with 1:* association to both account and tag. You can they either set filters on queries against the navigation properties, or you can create a simple hierarchy of SkillTag:Account2Tag and ExperienceTag:Account2Tag. For the base type, Account2Tag, you can provide a "bogus" condition for now, or, if writing the XML by hand, you can define it as abstract and not map it - instead, providing EntitySets only for SkillTag and ExperienceTag.

HTH,
 Noam


这篇关于EF多2对映射方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆