EF6:迁移生成可为 null 的字符串,尽管 IsRequired

EF6: migration generates nullable string despite IsRequired

我正在尝试为具有 string 字段的新实体 class 生成迁移,称为 "Name"。该字符串字段不应为空。

我知道 "nullable" 和 "non-empty" 是两个不同的问题(请参阅 EF 6 IsRequired() allowing empty strings)。但现在我只想坚持非空。

我没能做到。生成的迁移总是以 "nullable:true" 结束,如图所示:

    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Tags",
            columns: table => new
            {
                // ...
                Name = table.Column<string>(nullable: true),
                // ...
            },
            // ...
    }

我知道我可以通过注释 ( EF6 Add-Migration for not nullable string? ) 来实现这一点,但我们的架构师禁止这样做。我需要在 IEntityTypeConfiguration.

中使用 Configure 函数

我正在使用 IsRequired()

public class TagEntityTypeConfiguration : IEntityTypeConfiguration<Tag>
{
    public void Configure(EntityTypeBuilder<Tag> builder)
    {
        builder.ToTable("Tag");

        builder.HasKey(x => x.Id);

        builder.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(100);

    }
}

尽管有 .IsRequired,为什么我得到 nullable:true?

我只是忘记了使用 EntityTypeConfiguration:

        modelBuilder.ApplyConfiguration(new TagEntityTypeConfiguration());

没有这一行,约束将被忽略。