The Fluent API ValueGeneratedOnAdd Method

The Entity Framework Core Fluent API ValueGeneratedOnAdd method indicates that the value for the selected property is generated by the database whenever a new entity is added to the database. Therefore, the property should be ignored by EF Core when constructing an INSERT statement.

In the following example, the DateCreated property has been configured to map to a column that has a default value set. Therefore EF Core should not attempt to insert a default value for the property:

public class SampleContext : DbContext
    public DbSet<Contact> Contacts { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
            .Property(p => p.DateCreated)

    public class Contact
        public int Id { get; set; }
        public string FullName { get; set; }
        public string Email { get; set; } 
        public DateTime DateCreated { get; set; }

If a value is applied to a property that has the ValueGeneratedOnAdd method applied while the entity is being tracked by the context, and the value provided differs from the CLR default value for the property type, EF Core will attempt to insert that value.

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

Data Annotations

The Data Annotations attribute equivalent of the ValueGeneratedOnAdd method is the DatabaseGenerated attribute with the Identity option.

Previous Versions

In previous versions of Entity Framework, you would use the HasDatabaseGenerated method with the DatabaseGenerated.Identity enumeration to achieve the same outcome as the Entity Framework core ValueGeneratedOnAdd method.

© 2021 - Mike Brind.
All rights reserved.
Contact me at