DatabaseGenerated Attribute EF Core

Use EF Core DatabaseGenerated Attribute on a property whose value is automatically generated by the Database. This attribute is part of the System.ComponentModel.DataAnnotations.Schema Namespace.

DatabaseGenerated Attribute

You may have computed fields in your database. The database updates the computed fields when you insert or update the data. For Example, database inserts the new values for Identity or Guid columns, when you insert the data. The inserted data must be updated in the model, once the saveChanges is called 

DatabaseGeneratedOption

This attribute has three options

Identity: The database generates value when we insert the row.
Computed: Database generates a value for the property when we insert or update the row.
None: Database does not generate a value for the property either in insert or in an update.

Note that how DatabaseGenerated implemented differs from the one database provider to another database provider. The Example in this article uses the SQL Server

DatabaseGenerated.None

This prevents the database from creating the computed values. The user must provide the value.

This is useful when you want to disable the generation of identity for the integer primary key. The following domain model Customer, with the DatabaseGeneratedOption.None attribute will create the CustomerID column with identity disabled.

DatabaseGenerated.Identity

When we apply Identity attribute to a property, the entity framework expects that the database will compute its value when we insert a new row.

If we apply this attribute to the numeric property, the Entity Framework will create the identity column in the database. Remember that you can have only one identity column in the database.

The above will create the SrNo as Identity column in the database. The entity framework also retrieves the computed value from the database after the insert.

When you insert the data into the table, the EF generates the following queries. Note that the insert query does not contain SrNo field. Once inserted, the EF attempts to read the SrNo from the Database, so as to update the SrNo field.

While in case of update the EF generates the following query. The SrNo is not in the Insert query, because EF knows that the database generates the value only when inserting the values.

DatabaseGenerated.Computed

This is useful in scenarios where you have computed columns in your database. The Database computes the value of these fields after each insert/update operation. The entity framework will not update these columns. But it will query and return the values of these fields after an insert or update operation.

In the above model, we have two computed fields. CustomerNameCreated.

Use the following code to insert data into the table. Both customerName & created fields are given value.

The FE generates the following SQL statement. As you can see the EF does not insert the values to the customerName  & Created fields, but it tries to retrieve the value of these fields immediately after the insert. i.e. because EF expects that the database to compute the value of these fields.

Similarly, the update query does not update the computed fields but retrieves them after the update expecting that the database may have computed and updated these fields

In both, the above examples, the database inserts the null value into the CustomerName & Created fields as we are not generating any values for them in the database. For testing purposes, you can use the following insert trigger to insert values and check the result.

References

  1. DatabaseGeneratedAttribute Class

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top