开发者

AssociationSet in EF4

开发者 https://www.devze.com 2023-02-06 14:54 出处:网络
I am a beginner in EF4 , and I am not able to understand something in .edmx file , Could anyone explain to me what is the benefit of AssociationSet ,I think Association element represents 开发者_如何

I am a beginner in EF4 , and I am not able to understand something in .edmx file ,

Could anyone explain to me what is the benefit of AssociationSet ,I think Association element represents 开发者_如何学编程everything about the relationship between the tables , so why should I use AssociationSet .

Thanks,


The AssociationSet is a container for an association.

Lets began from a simle case when we have a Contract and Address types. Its relationship could be described in xml like:

<Association Name="FK_Address_Contact">
  <End Role="Contact" Type="SampleModel.Contact" Multiplicity="1">
    <OnDelete Action="Cascade" />
  </End>
  <End Role="Address" Type="SampleModel.Address" Multiplicity="*" />
  <ReferentialConstraint>
    <Principal Role="Contact">
      <PropertyRef Name="ContactID" />
    </Principal>
    <Dependent Role="Address">
      <PropertyRef Name="ContactID" />
    </Dependent>
  </ReferentialConstraint>
</Association>

As you can see here, one Contact can have many Addresses. We here describe that there exists a reference between two types. But how we can describe a case when in Contract type we can have, actually, two or more addresses. For example, WorkAddress and HomeAddress. Within Association we can describe only fact that two types have referenced each other, but within AssociationSet we can also describe that one type could use two equal references to other type.

For such case we can define next xml:

<EntityContainer Name="ContactsContainer" >
  <EntitySet Name="WorkContacts" EntityType="SampleModel.Contact" />
  <EntitySet Name="HomeContacts" EntityType="SampleModel.Contact" />
  <EntitySet Name="WorkAddresses" EntityType="SampleModel.Address" />
  <EntitySet Name="HomeAddresses" EntityType="SampleModel.Address" />
  <AssociationSet Name="ToWorkAddress" Association="SampleModel.FK_Address_Contact">
    <End Role="Contact" EntitySet="WorkContacts" />
    <End Role="Address" EntitySet="WorkAddresses" />
  </AssociationSet>
  <AssociationSet Name="ToHomeAddress" Association="SampleModel.FK_Address_Contact">
    <End Role="Contact" EntitySet="HomeContacts" />
    <End Role="Address" EntitySet="HomeAddresses" />
  </AssociationSet>
</EntityContainer>
0

精彩评论

暂无评论...
验证码 换一张
取 消