0

In my project, I have a table:

[Table("users")]
public class User
{
    [Column("id"), Key, Required]
    public int Id { get; set; }

    [Column("name"), MaxLength(64)]
    public string Name { get; set; }
}

The property Name is not flagged with Required data annotation. However, in the generated migration, I see this code:

migrationBuilder.CreateTable(
    name: "users",
    columns: table => new
    {
        id = table.Column<int>(type: "int", nullable: false)
        .Annotation("MySQL:ValueGenerationStrategy", MySQLValueGenerationStrategy.IdentityColumn),
        name = table.Column<string>(type: "varchar(64)", maxLength: 64, nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_users", x => x.id);
    })
    .Annotation("MySQL:Charset", "utf8mb4");

The nullable property of the Name column should be true as long as I am not marking it as Required, shouldn't it?

I am using EF Core 7.0.7 with the MySql.EntityFrameworkCore 7.0.2 (newest) package.

How to fix this?

Of course I can manually replace the nullable property by hand in the migration file, but that's a lot of work and offers potential for mistakes...

I also know the FluentAPI declarations, but I prefer using data annotations.

With FluentAPI the nullable property in the migration file is set correctly.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

0 Answers0