Search code examples
entity-frameworkef-code-firsttable-per-type

Entity Framework 4.1, Table Per Type Inheritance and Lookup Tables


Using table per type inheritance is it possible to have the same data with the same key in more than one of the derived types?

e.g.

Base Type :

[Table("BaseType")]
public abstract class BaseType
{
    public int Id { get; set; }
    public string Description { get; set; }
}

Derived Type A :

[Table("DerivedTypeA")]    
public class DerivedTypeA : BaseType {}

Derived Type B :

[Table("DerivedTypeB")]
public class DerivedTypeB : BaseType {}

Along with a number of types that are either DerivedTypeA or DerivedTypeB I have one particular type that is both DerivedTypeA and DerivedTypeB

When I seed the database by creating a new DerivedTypeA and DerivedTypeB for this type it gets added twice, with different Ids. While not the end of the world, I'd like it to have the same Id in the two derived tables.

Is this possible?

Thanks in advance.


Solution

  • If I understand you correctly, you're describing a multiple-inheritance situation, which isn't supported in C#. DerivedA cannot be DerivedB or vice-versa because they are siblings.

    This article helps define the issue and presents a pattern which might help you: http://www.codeproject.com/KB/architecture/smip.aspx