Entity Framework Core 2.0 default value for common property in many tables

asp.net-core-2.0 entity-framework-core

Question

I am doing EF Core 2.0 migrations using code first. I have a few columns that apply to all entities. As illustrated below, all entities inherit from this class.

How can I make the current date the default setting for CreateDate, "System" for CreateBy, and "0" for IsDeleted?

public class BaseEntity
{
    public DateTime CreateDate { get; set; }

    public string CreateBy { get; set; }

    public bool IsDeleted { get; set; }
}

I understand how to handle one property at a time. 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 somebody advise me on how to do this quickly such that it is accessible to all entities?

Thanks

1
3
1/10/2018 12:03:32 PM

Accepted Answer

ZZZ_tmp
2
2/8/2018 11:54:37 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow