简体   繁体   中英

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?

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.

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.

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());
    {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM