EF Core HasDefaultValue

The Entity Framework Core Fluent API HasDefaultValue method is used to specify the default value for a database column mapped to a property. The value must be a constant.

public class Contact
    public int ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public bool IsActive { get; set; }
    public DateTime DateCreated { get; set; }

public clas SampleContext : DbContext
    public DbSet<Contact> Contacts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
            .Propery(p => p.IsActive)

Data Annotations

It is not possible to configure default database column values using data annotations.

Date Modified: 2023-02-26

Edit this page in GitHub

Got any EF Core Question?