Export (0) Print
Expand All

How to: Create an Entity Key When No Key Is Inferred

[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 edit an .edmx file to change an entity key when the Entity Data Model Wizard or the Update Model Wizard could not infer an entity key.

When you generate or update an .edmx file with the Entity Data Model Wizard or the Update Model Wizard, the key of an entity type is mapped to the primary key of the corresponding table. If no primary key is defined (for example, in a view), the tools will attempt to infer a unique entity key based on the columns of the table or view. The inferred key will be composed of all non-nullable and non-binary columns of the table or view. If the table or view contains only nullable and binary columns, no key will be inferred.

If no entity key is inferred, the entity will not be added to the model. Instead, a code comment that contains the EntityType element (with no defined key) will be added to the SSDL section of the .edmx file.

For more information about entity keys, see EntityType Element (CSDL) and EntityType Element (SSDL).

The procedures in this topic assume that you have a valid .edmx file open in Visual Studio.

Dd163156.note(en-us,VS.110).gifNote:
Changes made to the SSDL section of an .edmx file, as suggested in the procedures below, will be overwritten if you use the Update Model Wizard to update your model. To avoid manually editing the .edmx file in this case, modify your database schema so that each table has a primary key or so that one or more columns of each table or view is non-nullable or non-binary.

Adding an Entity Key When No Key Is Inferred from a Table

The following procedure applies to an entity type that corresponds to a table. This procedure assumes that the .edmx file is open in the XML Editor.

If the Entity Designer cannot infer an entity key based on the columns of a table, the entity will not be added to the model. Instead, a code comment that contains the EntityType element (with no defined key) will be added to the SSDL section of the .edmx file.

To add an entity key when no key is inferred from a table

  1. Uncomment the EntityType element in the SSDL section of the .edmx file.

  2. Define the key for the entity type by adding the appropriate Key elements to the EntityType element. For more information, see EntityType Element (SSDL).

    Dd163156.note(en-us,VS.110).gifNote:
    The column or columns used to define the entity key must uniquely identify a row in the table.

  3. In the SSDL section, add an EntitySet element that specifies the table to which the added entity type maps. For more information, see EntitySet Element (SSDL).

  4. In the CSDL section, add an EntityType element that corresponds to the added SSDL entity type. For more information, see EntityType Element (CSDL).

  5. In the C-S (conceptual to storage) mapping section, add an EntitySetMapping element that specifies the mapping between the added SSDL and CSDL entity types. For more information, see EntitySetMapping Element (MSL).

Adding an Entity Key When No Key Is Inferred from a View

The following procedure applies to an entity type that corresponds to a view. This procedure assumes that the .edmx file is open in the XML Editor.

If the Entity Designer cannot infer an entity key based on the columns of a view, the entity will not be added to the model. Instead, a code comment that contains the EntityType element (with no defined key) is added to the SSDL section of the .edmx file.

To add an entity key when no key is inferred from a view

  1. Uncomment the EntityType element in the SSDL section of the .edmx file.

  2. Define the key for the entity type by adding the appropriate Key elements to the EntityType element. For more information, see EntityType Element (SSDL).

    Dd163156.note(en-us,VS.110).gifNote:
    The column or columns used to define the entity key must uniquely identify a row in the view.

  3. Add a defining query to the model. For more information, see How to: Add a Defining Query.

See Also

Show:
© 2014 Microsoft