I am doing code first EF Core 2.0 migrations. I have few columns which are common across all entities. All entities inherit this class as shown below.
How do I set the default value for CreateDate as current date, CreateBy as 'System' and IsDeleted as 0?
public class BaseEntity
{
public DateTime CreateDate { get; set; }
public string CreateBy { get; set; }
public bool IsDeleted { get; set; }
}
I know how to do this for a single property. ie.
class MyContext : DbContext
{
public DbSet<CaseDetails> CaseDetails{ get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CaseDetail>()
.Property(b => b.CreateDate)
.HasDefaultValueSql("getutcdate()");
modelBuilder.Entity<CaseDetail>()
.Property(b => b.CreateBy)
.HasDefaultValue("System");
modelBuilder.Entity<CaseDetail>()
.Property(b => b.IsDeleted)
.HasDefaultValue("0()");
}
}
Can anyone help me how to do this in an efficient way so it will be available for all entities?
Thanks