locked
One Table Per Class Inheritance RRS feed

  • Question

  • Hi.

    I'm testing Ado.Net vNext inheritance features.
    In my SqlSrv DB I've a one-table-per-class inheritance structure,
    with one table for Base objects and one table for each Derived objects,
    linked to the base through the ID field

    Base(ID, BaseProperty)
    Derived(ID, DerivedProperty)
    .....

    SSDL

    <Schema Namespace="AdoNetEntityFrameworkTestTarget" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
      <EntityType Name="Base" Key="ID">
        <Property Name="ID" Type="int" Nullable="false"/>
        <Property Name="BaseProperty" Type="nvarchar" Nullable="false" MaxLength="50"/>
      </EntityType>
      <EntityContainer Name="dbo">
        <EntitySet Name="Base" EntityType="AdoNetEntityFrameworkTestTarget.Base"/>
        <EntitySet Name="Derived" EntityType="AdoNetEntityFrameworkTestTarget.Derived"/>
      </EntityContainer>
      <EntityType Name="Derived" Key="ID">
        <Property Name="ID" Type="int" Nullable="false" />
        <Property Name="DerivedProperty" Type="nvarchar" Nullable="false" MaxLength="50"/>
      </EntityType>
    </Schema>



    In the Entity Model, I've created a Base entity and a Derived entity

    CSDL


    <Schema Namespace="AdoNetEntityFrameworkTest.Inheritance" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
      <EntityType Name="Base" Key="ID">
        <Property Name="ID" Type="Int32" Nullable="false"/>
        <Property Name="BaseProperty" Type="String" Nullable="false" MaxLength="50"/>
      </EntityType>
      <EntityType Name="Derived" BaseType="Self.Base">
        <Property Name="DerivedProperty" Type="String" Nullable="false" MaxLength="50"/>
      </EntityType>
      <EntityContainer Name="InheritanceDB">
        <EntitySet Name="BaseSet" EntityType="Self.Base"/>
        <EntitySet Name="DerivedSet" EntityType="Self.Derived"/>
      </EntityContainer>
    </Schema>

    My problem is write the MSL file for this scenario.

    Can anyone help me?

    Thanks
    Roberto.




    Wednesday, October 25, 2006 2:43 PM

Answers

All replies

  • Hi Roberto,

    I have posted an earlier example on how to map a class hierarchy to a single table - http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=641069&SiteID=1

    You can use that as a guide for your specific scenario.
    Please let us know if you have problems.
    Thanks

    Wednesday, October 25, 2006 8:20 PM
  • Thanks for the example you've posted! Great!

    I've solved my problem ...
    by the way in that model there is only one entityset
    for the Base (Book in that case) entitytype, so if I want a set of
    Derived (SciFiBook) entitytype I've to write (with Linq help :-))

    from b
    in BooksContainer.Books
    where b is SciFiBook
    select b

    Is it possible to have an entityset for the Derived entitytype?

    Thanks.
    Roberto

    Thursday, October 26, 2006 2:10 PM
  • For now you will want to keep the derived types in the same entityset as the base type: Once you have more than one derived type, the roundtripping will fail, since there is no way to guarantee that two types do not have the same base-type ID.

    e.g.

    BaseType.ID : 16

    DerivedType1.ID : 16
    DerivedType2.ID : 16

    You will get a runtime exception that looks something like this:

    Problem in Mapping Fragment(s) starting at line(s) (6): In the mapping for table Items
    The tuples in table Items obtained from the fragment on line 6 are the same as the table tuples specified in the fragment on line 21. However, the corresponding constraint is not true on the C-side -- the former set has some entities that are not present in the latter set.

    The tuples in table Items obtained from the fragment on line 21 are the same as the table tuples specified in the fragment on line 6. However, the corresponding constraint is not true on the C-side -- the former set has some entities that are not present in the latter set.

    Monday, October 30, 2006 10:22 PM