实体框架6.1.3设置默认值 [英] Entity framework 6.1.3 set default value

查看:93
本文介绍了实体框架6.1.3设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程.

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 不可用.我试图通过构造函数进行设置,结果相同.

How can I set default value for property IsDeleted using Fluent API. Method HasDefaultValue is not available. I have tried to set via constructor, the same result.

推荐答案

如果您使用的是C#6 +,它将添加

If you are using C#6 +, it has added the ability to assign a default value to auto-properties. So you can simply write something like this:

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

当将此列添加到数据库时,它将默认值设置为false.

This will set the default value to false when this column is added to your database.

这篇关于实体框架6.1.3设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆