问题描述
我有一个具有审计属性的基类,例如
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; }
}
我所有的poco课程都来自于该课程.
All my poco classes derive from this class.
我正在尝试将默认值设置为IsActive属性.我不热衷于使用注释,因此如果我能使用流利的API来解决这个问题,那我就无所适从.
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.
我尝试了此操作,但没有用.好像它创建了一个名为BaseModel的新表
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?
推荐答案
无法执行此操作.它无法使用Entity Framework设置默认值.相反,您可以使用构造函数
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;
}
}
这篇关于EF 6 Codefirst-使用Fluent API为基类中定义的属性设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!