如何使用Entity Framework 6 Code First设置默认值约束? [英] How can set a default value constraint with Entity Framework 6 Code First?

查看:157
本文介绍了如何使用Entity Framework 6 Code First设置默认值约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在旧版应用程序中,大多数字符串属性不能为空,并且需要具有默认值string.empty。

In a legacy app, most string properties can't be null and need to have a default value of string.empty.

我知道可以这样做使用迁移,但我正在寻找一种方法来使用流畅的配置界面:

I know it's possible to do this with migrations, but I'm looking for a way to do this using the fluent configuration interface:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<string>().Configure(c =>
        {
            c.HasMaxLength(255);

            if (!c.ClrPropertyInfo.IsDefined(typeof (NullableAttribute), false))
            {
                c.IsRequired();
                // I want to set a default value (string.empty) here.
            }
    }

有没有办法这样做或我'注定初始化实体构造函数中的所有字符串?

Is there any way to do this or I'm doomed to initialize all strings in the entity constructors?

推荐答案

现在的答案是

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

这篇关于如何使用Entity Framework 6 Code First设置默认值约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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