Export (0) Print
Expand All

How to: Define a Model with a Single Entity Mapped to Two Tables

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic describes how to manually create a conceptual model in which an entity type is mapped to two tables in the underlying database. You can use these same concepts to map an entity type to more than two tables.

Bb896233.note(en-us,VS.110).gifNote:
The recommended way to define a model with an entity type mapped to multiple tables is by using the ADO.NET Entity Data Model Tools. For more information, see Walkthrough: Mapping an Entity to Multiple Tables.

You should only map an entity type to multiple tables if the following conditions are true:

  • The tables to which you are mapping share a common key.

  • The entity type that is being mapped has entries in each underlying table. In other words, the entity type represents data that has a one-to-one correspondence between the two tables; the entity type represents an inner join of the two tables.

The basic steps for manually defining a model with an entity mapped to two tables are as follows:

  1. Define an entity type with properties that correspond to columns in each of the underlying tables. For more information, see EntityType Element (CSDL).

  2. Define the mapping for the entity type by using a MappingFragment element for each underlying table. For more information, see EntityTypeMapping Element (MSL) and MappingFragment Element (MSL).

The following example assumes that you have installed the School sample database and that you have manually configured your project to use the Entity Framework. For more information, see Creating the School Sample Database and Configuring the Entity Framework.

Bb896233.note(en-us,VS.110).gifNote:
The example below will map an entity type to the Person and OfficeAssignement tables in the School sample database. Note that the conditions for mapping an entity type to these tables are met. Namely, an inner join of the two tables will result in all people that have an office assignment.

To create the storage model

  1. Add the following XML file to your project and name it School.ssdl.

To create the conceptual model

  1. Add the following XML file to your project and name it School.csdl. Note the following:

    • The Instructor entity type has properties that map to all columns if the Person and OfficeAssignment tables (see the storage model above for details about the tables).

      Bb896233.note(en-us,VS.110).gifNote:
      Because the Instructor entity type is an inherited type, it inherits all the properties from its base type, Person. Together with the properties that are unique to the Instructor type, the Instructor entity type as properties that map to all columns of the underlying tables.

To define the mapping between the conceptual model and the storage model

  1. Add the following XML file to your project and name it School.msl. Note the following:

    • Within the EntityTypeMapping element for the Instructor entity type, separate MappingFragment elements are used to map properties to the appropriate tables.

See Also

Show:
© 2014 Microsoft