EF Core DatabaseGenerated Attribute

The DatabaseGenerated attribute specifies how values are generated for a property by the database. The attribute takes a DatabaseGeneratedOption enumeration value, which can be one of three values:

  1. Computed
  2. Identity
  3. None

Values can be generated by the database when data is added to the database, or when it is added or updated (saved).

Computed

The Computed option specifies that the property's value will be generated by the database when the value is first saved, and subsequently regenerated every time the value is updated. The practical effect of this is that Entity Framework will not include the property in INSERT or UPDATE statements, but will obtain the computed value from the database on retrieval.

Entity Framework Core will not implement a value generation strategy. Database providers differ in the way that values are automatically generated. Some will generate values for selected data types such as Identity, rowversion, GUID. Others may require manual configuration such as setting default values or triggers, or configuring the column as Computed.

language-csharp
|
public class Contact
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; } 
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime LastAccessed { get; set; }
}

Identity

The Identity option specifies that the value will only be generated by the database when a value is first added to the database. Thereafter, the property will not be included in UPDATE statements by Entity Framework.

Again, Entity Framework Core will not implement a value generation strategy. Database providers differ in the way that values are automatically generated. Some will generate values for selected data types such as Identity, rowversion, GUID.

In the following example, the annotation is used to configure the Created property in the Contact entity. Since the property is required, Entity Framework will configure a default value of DateTime.MinValue. You can override this by setting a default value in the entity's constructor or by initializing an auto-implemented property (as illustrated here):

language-csharp
|
public class Contact
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; } 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public DateTime Created { get; set; } = DateTime.UtcNow;
}

Alternatively, you can configure a default value in the database itself.

None

The None option prevents values from being generated by the database automatically in cases where they would otherwise be created. Convention specifies that a property named 'Id' with a data type of int will be configured as the primary key for an entity, and will be mapped to an Identity column in SQL Server, which automatically generates values when data is added. If you want to implement your own value generation strategy and override the default behaviour, you will use the None option:

language-csharp
|
public class Car
{
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int Id { get; set; }
    public string Model { get; set; }
    public DateTime Registered { get; set; }
}

Fluent API

The Fluent API equivalent mehtods for the DatabaseGenerated attribute are


Date Modified: 2023-02-28
Author:

Edit this page in GitHub

Got any EF Core Question?