One to one relationship to same table.

I can't configure a CSDL and MSL files to create a relationship of type:

Person.Spouse

Where, Person and Spouse are of same Type, and Same Table.

On table there are the fields PersonID and SpouseID.

I want that when I reference the persons, example: john.Spouse = anna;automatically anna.Spouse becomes equals to john.

Is possible to create this relationship and could anyone help me to create?

Thanks

[533 byte] By [Vítor] at [2008-2-25]
# 1

I have included the example for self-association below. Using this example, you can run queries and updates. Let us know if you have any problems or more questions. Note that if you add a foreign key constraint in the store from SpouseId to PersonId (foreign key into the same table), the update pipeline will currently throw an exception.

CSDL

<?xml version="1.0" encoding="utf-8"?>

<Schema Namespace="SelfAssociation.Model" Alias="Self" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:edm="http://schemas.microsoft.com/ado/2006/04/edm" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">

<EntityContainer Name="SelfAssociation_Container">

<EntitySet Name="CPerson1" EntityType="Self.CPerson" />

<AssociationSet Name="CSpouseOf1" Association="Self.CSpouseOf">

<End Role="Person" EntitySet="CPerson1" />

<End Role="Spouse" EntitySet="CPerson1" />

</AssociationSet>

</EntityContainer>

<EntityType Name="CPerson" Key="PersonId">

<Property Name="PersonId" Type="Int32" Nullable="false" />

<Property Name="Name" Type="String" MaxLength="32" />

</EntityType>

<Association Name="CSpouseOf">

<End Role="Person" Type="Self.CPerson" Multiplicity="1" />

<End Role="Spouse" Type="Self.CPerson" Multiplicity="1" />

</Association>

</Schema>

SSDL

<?xml version="1.0" encoding="utf-8"?>

<Schema Namespace="SelfAssociation.Model" Alias="Self" xmlns:edm="http://schemas.microsoft.com/ado/2006/04/edm/ssdl" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">

<EntityContainer Name="SelfAssociation_dbo">

<EntitySet Name="SPerson1" EntityType="Self.SPerson" />

</EntityContainer>

<EntityType Name="SPerson" Key="PersonId">

<Property Name="PersonId" Type="int" Nullable="false" />

<Property Name="Name" Type="nvarchar" MaxLength="1024" />

<Property Name="SpouseId" Type="int" />

</EntityType>

</Schema>

MSL

<?xml version="1.0" encoding="utf-8"?>

<Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" xmlns:cdm="urn:schemas-microsoft-com:windows:storage:mapping:CS" cdm:Space="C-S">

<Alias cdm:Key="C" cdm:Value="SelfAssociation.Model" />

<Alias cdm:Key="S" cdm:Value="SelfAssociation.Target" />

<EntityContainerMapping cdm:CdmEntityContainer="SelfAssociation_Container" cdm:StorageEntityContainer="SelfAssociation_dbo">

<EntitySetMapping cdm:Name="CPerson1" cdm:TypeName="C.CPerson" cdm:TableName="SPerson1">

<ScalarProperty cdm:Name="PersonId" cdm:ColumnName="PersonId" />

<ScalarProperty cdm:Name="Name" cdm:ColumnName="Name" />

</EntitySetMapping>

<AssociationSetMapping cdm:Name="CSpouseOf1" cdm:TypeName="C.CSpouseOf" cdm:TableName="SPerson1">

<EndProperty cdm:Name="Person">

<ScalarProperty cdm:Name="PersonId" cdm:ColumnName="PersonId" />

</EndProperty>

<EndProperty cdm:Name="Spouse">

<ScalarProperty cdm:Name="PersonId" cdm:ColumnName="SpouseId" />

</EndProperty>

</AssociationSetMapping>

</EntityContainerMapping>

</Mapping>

AtulAdya-MSFT at 2007-10-7 > top of Msdn Tech,Visual Studio Orcas,ADO.NET Orcas...

Visual Studio Orcas

Site Classified