如何设置C首先实体框架6 $ C $默认值约束?实体、默认值、如何设置、框架

2023-09-04 22:35:07 作者:爱咬吸管的少女

在一个旧的应用程序,大部分的字符串属性不能为空,需要的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));
 
精彩推荐
图片推荐