Skip to content

EF set nvarchar(450) in case of having index for string property #31167

Description

@sa-es-ir

I have a model like this:

public class Student
{
    public int Id { get; set; }

    public string UniqueCode { get; set; }
}

Configuration:

 modelBuilder.Entity<Student>()
            .HasKey(x => x.Id);

  modelBuilder.Entity<Student>()
            .HasIndex(x => x.UniqueCode)
            .IsUnique();

And generated migrations:

migrationBuilder.CreateTable(
                name: "Students",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UniqueCode = table.Column<string>(type: "nvarchar(450)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Students", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Students_UniqueCode",
                table: "Students",
                column: "UniqueCode",
                unique: true,
                filter: "[UniqueCode] IS NOT NULL");

EF set the string property to nvarchar(450), I know maybe EF is wise enough to know that SQL will not allow creating an index on nvarchar(max) but why 450 chars? why not at least give a warning during generating migrations?

Include provider and version information

EF Core version: EF 7.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions