Export (0) Print
Expand All

How to: Work with Store-Generated Column Values

.NET Framework 3.5

This topic describes how to set entity property values that correspond to server-generated column values.

The StoreGeneratedPattern attribute allows you to control how the Entity Framework synchronizes database column values and entity property values. In particular, the StoreGeneratedPattern attribute of a Property element in the store schema definition language (SSDL) specifies whether the corresponding property value will be retrieved from the server. For more information, see Store Generated Pattern (SSDL) and Property Element (EntityType SSDL).

NoteNote

Changes made to the SSDL section of an .edmx file, as suggested in the procedure below, will be overwritten if you use the Update Model Wizard to update your model.

The following table describes the behavior for each of the possible values of the StoreGeneratedPattern attribute:

StoreGeneratedPattern value Description

Computed

If you create a new entity or change an existing entity, the values of properties with StoreGeneratedPattern set to Computed are retrieved from the server when you call the SaveChanges method in your application.

If you assign a value to a property with StoreGeneratedPattern set to Computed in your application, the value will be overwritten with the server-generated value when you call the SaveChanges method.

Identity

If you create a new entity, the values of properties with StoreGeneratedPattern set to Identity are retrieved from the server when you call the SaveChanges method in your application.

If you modify an entity, the values of properties with StoreGeneratedPattern set to Identity are assumed to be unchanged and are not retrieved from the server when you call the SaveChanges method.

If you assign a value to a property with StoreGeneratedPattern set to Identity in your application, the value will be overwritten with the server-generated value when you call the SaveChanges method.

None

The values of properties with StoreGeneratedPattern set to None are not retrieved from the server when you call the SaveChanges method in your application. Setting the value to None has the same effect as not including the StoreGeneratedPattern attribute in the Property element.

NoteNote

The Entity Data Model Wizard and Update Model Wizard will set the StoreGeneratedPattern attribute according to column types in the database. However, in some cases these wizards cannot determine whether a column value is computed on the server. In these cases, setting the StoreGeneratedPattern manually is recommended.

Adding or Editing a StoreGeneratedPattern Attribute

This procedure assumes that you have an .edmx file open in the XML Editor in Visual Studio.

To add or edit a StoreGeneratedPattern attribute

  1. In the Storage Model Content section of the .edmx file, find the Property element to which you want to add the StoreGeneratedPattern attribute.

  2. Add or edit the StoreGeneratedPattern attribute. Set its value to one of the values in the table above. For example, StoreGeneratedPattern="Computed".

See Also

Community Additions

Show:
© 2014 Microsoft