Code First. Splitting Entity Into Two Tables

Entity Framework allows to divide a single entity to 2 separate physical tables. To do this you need to declare an entity as usual, and add a Fluent API command to map this entity to tables.

Declaration of entity:

Table annotation is not necessary here and will be omitted by Visual Studio.

Entity-table mapping:

After migration Entity Framework will create 2 tables with the names defined in OnModelCreating function.

In Entity Framework you work with a single entity, i.e. you can create/update entity as usual, but when you save changes, the database context will generate separate SQL commands to modify the corresponding tables (first, second or both).

Be aware if you skip a property in mapping, Entity Framework will create a 3rd table with key field and a new created one.

Leave a Reply

Your email address will not be published. Required fields are marked *