EF 6 Codefirst -Setting default value for a property defined in base class using fluent API

两盒软妹~` 提交于 2020-01-04 13:36:17

问题


I have a base class which has audit properties like

public abstract class BaseModel
{
    [Column(Order = 1)] 
    public long Id { get; set; }
    public long CreatedBy { get; set; }
    public DateTime CreatedDate { get; set; }
    public long ModifiedBy { get; set; }
    public DateTime ModifiedDate { get; set; }
    public bool IsActive { get; set; }
}

All my poco classes derive from this class.

I am trying to set a default value to the IsActive properties. I am not keen on using annotations and hence was wandering if I can work this using fluent API.

I tried this but it does not work. Seems like it creates a new table named BaseModel

modelBuilder.Entity<BaseModel>()
    .Property(p => p.IsActive)
    .HasColumnAnnotation("DefaultValue", true);

Can any one suggest a way here?


回答1:


There is no way to do this. It can't set default values with Entity Framework. Instead you can use the constructor

public abstract class BaseModel
{
    protected BaseModel()
    {
        IsActive = true;
    }
}



回答2:


I have resolved this problem by overriding the SaveChanges method. See below for my solution.

  1. Solution Explain

    i) Override the SaveChanges method in the DbContext class.

    public override int SaveChanges()
    {
        return base.SaveChanges();
    }
    

    ii) Write logic to set default values

    public override int SaveChanges()
    {
        //set default value for your property
        foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("YOUR_PROPERTY") != null))
        {
            if (entry.State == EntityState.Added)
            {
                if (entry.Property("YOUR_PROPERTY").CurrentValue == null)
                    entry.Property("YOUR_PROPERTY").CurrentValue = YOUR_DEFAULT_VALUE;
            }
        }
    
        return base.SaveChanges();
    }
    
  2. Example

    i) Create Base Class

      public abstract class BaseModel
      {
           [Column(Order = 1)] 
           public long Id { get; set; }
           public long CreatedBy { get; set; }
           public DateTime CreatedDate { get; set; }
           public long ModifiedBy { get; set; }
           public DateTime ModifiedDate { get; set; }
           public bool IsActive { get; set; }
      }
    

    ii) override SaveChanges

    public override int SaveChanges()
    {
    
        //set default value for IsActive property
        foreach (var entry in ChangeTracker.Entries().Where(entry => entry.Entity.GetType().GetProperty("IsActive") != null))
        {
            if (entry.State == EntityState.Added)
            {
                if(entry.Property("IsActive").CurrentValue == null)
                    entry.Property("IsActive").CurrentValue = false;
            }
        }
    
        return base.SaveChanges();
    }
    


来源:https://stackoverflow.com/questions/30945133/ef-6-codefirst-setting-default-value-for-a-property-defined-in-base-class-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!