Search code examples
asp.net-mvcentity-frameworktable-per-type

Need help migrating existing model to include table per type inheritance


The application I'm working on is a simple application for keeping track of client information (creating, viewing, editing). I'd like to expand the application to be able to include basic information about family members of the clients.

I recently discovered the ability to use inheritance in entity framework and would like to create a simple inheritance hierarchy where clients inherit from a class called person because clients keep track of much of the same information as their family members plus some extra stuff. This way, if a family member also became a client they would just need a corresponding entry in the client table with the extra info.

Where I'm stuck is how to migrate the existing model to this new model. The changes in the model seem clear, something like this:

public class Person
{
     public Int32 ClientID { get; set; }
     ... other fields ...
}

[Table("Client")]
public class Client : Person
{
    ... client specific fields ...
}

but what changes need to be made to the context and elsewhere to successfully migrate to this new model? Just making these changes to the model came up with a blank migration when doing add-migration in NuGet.

Edit: The migration is no longer blank but adds a discriminator column which I understand is a part of table per hierarchy setup.


Solution

  • After some help on reddit, it looks like in my context where I had my onModelCreating method I needed

    modelBuilder.Entity<Person>();