How to: Edit Inferred Entity Keys

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 has incorrectly inferred an entity key or could not infer an entity key.

When you generate or update an Entity Data Model (EDM) 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 Key Attribute (EntityType CSDL) and Key Attribute (EntityType SSDL).

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

Note

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.

Change an Entity Key When the Wrong Key Is Inferred

The following procedure applies to an entity type that corresponds to a table or view.

To change an entity key when the wrong key is inferred

  1. Make sure the .edmx file is open in the Entity Designer. For each property that is incorrectly marked as a key, right-click the property and clear the Entity Key selection. For more information, see EntityType Element (CSDL).

  2. For each property that you want to make part of the entity key, right-click the property and select the Entity Key option.

  3. Save the .edmx file and close the Entity Designer.

  4. Open the .edmx file in the XML Editor, and copy the entity keys from the EntityType element in the CSDL section to the corresponding EntityType element in the SSDL section. For more information, see EntityType Element (SSDL).

Add 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).

    Note

    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 (EntityContainer 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).

Add 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).

    Note

    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

Concepts

.edmx File Overview