1

I am trying to control how Entity Framework 6 maps my class hierarchy into tables so that the properties in an abstract class in the middle of my hierarchy is mapped to the descendant types, not to its base class.

My class hierarchy is quite simple:

public abstract class BaseType
{
    public int Id { get; set; }
    public DateTime DateField { get; set; }
}

public abstract class DerivedAbstract : BaseType
{
    public string MapToChild { get; set; }
}

public class Concrete1 : DerivedAbstract
{
    public int Age { get; set; }
}

public class Concrete2 : DerivedAbstract
{
    public string Name { get; set; }
}

I have setup a simple table-per-type hierarchy:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<BaseType>();
    modelBuilder.Entity<Concrete1>().ToTable("Concrete1");
    modelBuilder.Entity<Concrete2>().ToTable("Concrete2");
}

And this gives me three tables: BaseTypes, Concrete1 and Concrete2. So far I am very happy, but my challenge is that the field MapToChild defined in the DerivedAbstract class is mapped down to the BaseTypes table instead of to both of the Concrete1 and Concrete2 tables.

This makes sense in most cases, but not in the project I am working on. So I am looking for a way to tell Entity Framework that I want the property to be mapped to the two tables Concrete1 and Concrete2 instead.

So far I have been unable to find a way to do this. Does Entity Framework even support it?

7
  • what happens if you add a class : public class Concrete3 : BaseType { public string prop {get; set;}} with modelBuilder.Entity<Concrete3>().ToTable("Concrete3");.
    – tschmit007
    Jan 28, 2015 at 13:58
  • Getting the same situation. MapToChild is still in the BaseTypes table Jan 28, 2015 at 14:22
  • Are you needing to do polymorphic queries?
    – PilotBob
    Jan 28, 2015 at 16:13
  • 1
    This so answer implies this isn't supported. stackoverflow.com/questions/5776635/…
    – PilotBob
    Jan 28, 2015 at 16:14
  • 1
    No, this isn't supported. In fact you want a mixture of TPT, where all common properties are in the base table, and TPC, where all types have their own tables (they "inherit" the properties from the base type). Ideally, you'd want to choose which properties inherit and which don't. There is no API for that, although technically, in DBMS terms, this wouldn't be a problem. But are you sure you want a base table for each type? TPT can have heavy performance impact because everything requires joins or dual inserts. Jan 28, 2015 at 21:54

1 Answer 1

0

This is happening because you are telling it to map BaseType. So it is creating base type as a TPH mapping and adding a discriminator.

If you remove the BaseType mapping you will get a Table for concrete1 and a table for concrete2 which I think it was you want. Right?

Ah... sorry, probably what you want. I can't map your classes so that BaseType is a table and have the MapToChild be in each concrete type. I assume you are doing this so you can query BaseType and get back multiple child types.

So, never mind.

2
  • No, I actually want the base class in a separate table. What I don't want is for all other abstract classes to be mapped to that table as well. I want their data in the tables for the concrete classes. Jan 28, 2015 at 15:55
  • 2
    Yes, I see that now, and corrected to never mind. You'll need someone that knows more about it than I to do this mapping, it is is possible. Of course, dropping the intermediate class would work, it's not the best answer.
    – PilotBob
    Jan 28, 2015 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.