Home > dotnet 4, Entity Framework > EF Code First: Table Per Type

EF Code First: Table Per Type

In this post I am going to show you another type of mapping inheritance relationship which is called Table Per Type and it is quite similar to the Table Per hierarchy from the last post. The only difference is that here all the inheritance types will have their own table rather than combining them into one table.

Let start with a class diagram and see what we are trying to map.

Student Faculty Class Diagram

Person.cs

public abstract class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

Student.cs

public class Student : Person
{
    public int CreditScore { get; set; }
}

Faculty.cs

public class Faculty : Person
{
    public int AnnuaLeave { get; set; }
}

PersonConfiguration.cs

public class PersonConfiguration : EntityTypeConfiguration<Person>
{
   public PersonConfiguration()
   {
      this.Map<Student>(x => x.ToTable("Student"))
          .Map<Faculty>(x => x.ToTable("Faculty"));
   }
}

POCOContext.cs

public class POCOContext : DbContext
{
   public DbSet<Person> People { get; set; }
   public DbSet<Student> Student { get; set; }
   public DbSet<Faculty> Faculty { get; set; }

   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
      modelBuilder.Configurations.Add(new PersonConfiguration());
   }
}

as you can see we just specify in our configuration class that each of the entity maps to an individual table. And now when you create the database context you will see the following relationship between the Person,Student and Faculty tables.

Per Table per Type Database diagram

So you can see mapping inheritance in EF Code First is relatively simple but be careful when choosing one over the other. For example inheritance is great and it just works but when it comes to reporting it can lead to some complication, so think from that point of view as well.

Advertisements
  1. roggss
    September 20, 2011 at 10:54 am

    Hi, how am I to use this in a controller? If i would like to add person?

    Can you give me an example?

    Coz Im having trouble here adding a record with my Member class and have a subclasses of Person and Business.

    What I want to accomplished is to create a site memberhip, in which classified as personal or business.

    Tables are:

    Member
    * DateApplied
    * DateMembered

    Person
    * LastName
    * FirstName
    * BirthDate

    Business
    * Name
    * TaxID
    * DateRegistered

    Can you give me an example of my requirements above especially on adding a record to the member table.

    Thanks

  2. roggss
    September 20, 2011 at 11:19 am

    roggss :
    Hi, how am I to use this in a controller? If i would like to add person?
    Can you give me an example?
    Coz Im having trouble here adding a record with my Member class and have a subclasses of Person and Business.
    What I want to accomplished is to create a site memberhip, in which classified as personal or business.
    Tables are:
    Member
    * DateApplied
    * DateMembered
    Person
    * LastName
    * FirstName
    * BirthDate
    Business
    * Name
    * TaxID
    * DateRegistered
    Can you give me an example of my requirements above especially on adding a record to the member table.
    Thanks

    In addition, it can not have both personal and business for a certain member.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: