O'Reilly logo

Programming Entity Framework: Code First by Rowan Miller, Julia Lerman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Allowing Multiple Entities to Map to a Single Table: aka Table Splitting

Often a database table has so many columns in it that some scenarios only require you to use a subset of them, while others require access to additional column data. When mapping an entity to such a table, you may find yourself wasting resources pulling back, materializing, and carrying around unused data. Table splitting helps solve this problem by allowing you to break up the columns of a single table across multiple entities. Chapter 14 of the second edition of Programming Entity Framework covers how to map table splitting in the EDM designer. Here you’ll learn how to split a table by configuring Code First.

Note

You may be more likely to want table splitting when mapping to an existing database where you find yourself with the scenario described above, though you might find that you want it even when letting Code First create the database for you.

Let’s say that the existing database stores the photo and caption for a person in the People table rather than the separate PersonPhotos table. Since we may want access to the person’s name and personal information more frequently than the photo, keeping the photo in a separate class will work out nicely. Example 5-7 provides a reminder of what the PersonPhoto class looks like, as well as the Photo property in the Person class. While we’re at it, let’s configure the Photo property to be an image, as we did for the Destination.Photo property in Chapter 2.

Example 5-7. The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required