entity-framework/ef6/modeling/designer/table-splitting.md
This walkthrough shows how to map multiple entity types to a single table by modifying a model with the Entity Framework Designer (EF Designer).
One reason you may want to use table splitting is delaying the loading of some properties when using lazy loading to load your objects. You can separate the properties that might contain very large amount of data into a separate entity and only load it when required.
The following image shows the main windows that are used when working with the EF Designer.
To complete this walkthrough, you will need:
This walkthrough is using Visual Studio 2012.
The Entity Designer, which provides a design surface for editing your model, is displayed. All the objects that you selected in the Choose Your Database Objects dialog box are added to the model.
In this section you will split the Person entity into two entities and then map them to a single table.
[!NOTE] The Person entity does not contain any properties that may contain large amount of data; it is just used as an example.
The next step requires the Mapping Details window. If you cannot see this window, right-click the design surface and select Mapping Details.
Select the HireInfo entity type and click <Add a Table or View> in the Mapping Details window.
Select Person from the <Add a Table or View> field drop-down list. The list contains tables or views to which the selected entity can be mapped. The appropriate properties should be mapped by default.
Select the PersonHireInfo association on the design surface.
Right-click the association on the design surface and select Properties.
In the Properties window, select the Referential Constraints property and click the ellipses button.
Select Person from the Principal drop-down list.
Press OK.
using (var context = new SchoolEntities())
{
Person person = new Person()
{
FirstName = "Kimberly",
LastName = "Morgan",
Discriminator = "Instructor",
};
person.HireInfo = new HireInfo()
{
HireDate = DateTime.Now
};
// Add the new person to the context.
context.People.Add(person);
// Insert a row into the Person table.
context.SaveChanges();
// Execute a query against the Person table.
// The query returns columns that map to the Person entity.
var existingPerson = context.People.FirstOrDefault();
// Execute a query against the Person table.
// The query returns columns that map to the Instructor entity.
var hireInfo = existingPerson.HireInfo;
Console.WriteLine("{0} was hired on {1}",
existingPerson.LastName, hireInfo.HireDate);
}
The following T-SQL statements were executed against the School database as a result of running this application.
The following INSERT was executed as a result of executing context.SaveChanges() and combines data from the Person and HireInfo entities
The following SELECT was executed as a result of executing context.People.FirstOrDefault() and selects just the columns mapped to Person
The following SELECT was executed as a result of accessing the navigation property existingPerson.Instructor and selects just the columns mapped to HireInfo