实体框架6.1.3设置默认值

时间:2018-01-30 14:45:14

标签: c# asp.net entity-framework

我有以下课程。

public class User 
{
    public int Id { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public byte[] RowVersion { get; set; }

    public bool IsDeleted { get; set; }

}

如何使用Fluent API为属性IsDeleted设置默认值。方法HasDefaultValue不可用。我试图通过构造函数设置相同的结果。

1 个答案:

答案 0 :(得分:2)

如果您使用的是C#6 +,则会添加the ability to assign a default value to auto-properties。所以你可以简单地写下这样的东西:

public bool IsDeleted { get; set; } = false;

将此列添加到数据库时,这会将默认值设置为false。