文章内容

2017/2/4 16:41:21,作 者: 黄兵

EntityFramework Code First FluentAPI DefaultValue in EF6.X

使用的是EF 6.0,采用Data Annotations的下列两种方式都效果,不知道还有没有别的书写方式?

方式1:

public partial class MyModel
{
    [DefaultValue(0)]
    public int Field1 { getset; }
}

方式2:

public partial class MyModel
{
    public MyModel
    {
        Field1 = 0;
    }
    public int Field1 { getset; }
}
如果采用Fluent API,该如何定义默认值呢?

参考方法:

public override void Up()
        {
            AddColumn("dbo.Comments", "UsefulPoint", c => c.Int(nullable: false,defaultValue:0));
        }

Good news, code first now supports this. In the "Up()" method of the generated migration, specify a default with the following syntax:

AddColumn("[table name]", "[column name]", c => c.Boolean(nullable: false, defaultValue: false));



分享到:

发表评论

评论列表