chief7
chief7

Reputation: 14383

EF Code First: Denormalize Inherited Type

I have the following entities:

Human

-Name

-Age

-Height

-Weight
SuperHuman : Human

-SuperPower

EF is creating the SuperHumans table with only the Power property and when queried joins to the Humans table. I want all the columns on the SuperHumans table.

Can EF 4.1 Code First be configured to do this?

Upvotes: 0

Views: 468

Answers (3)

chief7
chief7

Reputation: 14383

I just needed to configure the SuperHuman to map inherited properties, like so:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SuperHuman>().Map(m => m.MapInheritedProperties());
    {

Upvotes: 0

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

One way to do this is to make the relationship a Has-a relationship instead of an Is-a relationship.

public class SuperHuman
{
    public Human TheHuman { get; set; }
    public string SuperPower { get; set; }
}

The resulting database table should have all the fields of Human and SuperHuman (as long as you don't have a separate Human table, in which case a separate Human table will be created, and a Foreign Key will be added to link the tables together.)

Its limited in utility, as it breaks inheritance. You may find a better solution.


Edit: You could do something like this:

public class SuperHuman : Human
{
    public string Name 
    { 
        get { return base.Name; } 
        set { base.Name = value; } 
    }
}

Note: This isn't tested, and may not be the best solution. I include it here merely as a possible solution.

Upvotes: 0

Alec
Alec

Reputation: 1706

Wouldn't making Human partial work?? If that doesn't work just look into Table-Per-Hierarchy vs Table-Per-Type vs Table-Per-Concrete-Type (TPH, TPT, TPC). You are currently using TPT and you want TPC.

Upvotes: 1

Related Questions