EF6 Add-Migration是否不能为空字符串? [英] EF6 Add-Migration for not nullable string?

查看:219
本文介绍了EF6 Add-Migration是否不能为空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会先从下面的代码中按代码添加NOT NULL NVARCHAR列:

I would add a NOT NULL NVARCHAR column by code first from below code:

public class ApplicationUser : IdentityUser
{
    [Required, MaxLength(10), MinLength(5)] 
    public string StaffCode { get; set; }
}

然后我运行添加迁移用户 在Nuget控制台上,

Then i run "Add-Migration User" on Nuget console,

它将在下面生成迁移类:

It generate the migrate class below:

public partial class abc : DbMigration
{
    public override void Up()
    {
        AlterColumn("dbo.AspNetUsers", "StaffCode", c => c.String(maxLength: 10));
    }

    public override void Down()
    {
        AlterColumn("dbo.AspNetUsers", "StaffCode", c => c.String());
    }
} 

最小长度:5 并且 nullable:false 不见了。
我称之为手动添加并更新到数据库,但这似乎不合理。

The minlength:5 and nullable:false is gone. I call add it manually and update to database, but it seems not reasonable.

谢谢。

推荐答案

我会回答我的问题。
i找到该帖子:
http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx

I would answer my question. i find that post: http://blogs.msdn.com/b/alexj/archive/2009/04/15/tip-12-choosing-an-inheritance-strategy.aspx

默认情况下,EF6使用 TPH ,不允许将列设置为NOT NULL。
请考虑每种类型的表(TPT)或每种具体类别的表(TPC)

By default EF6 using TPH ,which not allow set the column to NOT NULL. Please conside Table Per Type (TPT) or Table Per Concrete Class (TPC)

这篇关于EF6 Add-Migration是否不能为空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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