'How to add maxlength to an existing field using entity framework core 6?

I'm struggling to apply a max length on an existing field using entity framework core 6 with sql server. I have this existing field:

[Required]
public string MyField { get; set; }

In the code behind the latest migration file, the field looks like this:

b.Property<string>("MyField")
    .IsRequired()
    .HasMaxLength(100000)
    .HasColumnType("nvarchar(max)");

My fist question is this: How does HasMaxLength and HasColumnType("nvarchar(max)") play together here?

My goal is to apply a max length to this column, so that it's no longer of type nvarchar(max), but has a max length of 20,000 (as my current biggest entry in the column is about 10,000 characters).

I was hoping this would be as easy as doing this:

[Required]
[MaxLength(20000)]
public string MyField { get; set; }

And then creating a new migration. However, the new migration (created with dotnet ef migrations add RestrictMyFieldLength) is completely empty.

My second question is this: Why is the migration that's created empty?

I've been googling trying to understand this, and I came across this page. I tried deleting all my existing migrations and creating a new one (adding [MaxLength(20000)]), but it has no effect. The migration that's created still describes the field as nvarchar(max).

I'm using these versions:

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">

Update:

A colleague helped me figure out one blocker. I was configuring something different in my DbContext class, in the OnModelCreating method, which was overriding what I specified in the entity class. In the OnModelCreating method I had this:

modelBuilder.Entity<MyEntity>().Property(x => x.MyField).HasMaxLength(100_000);

Commenting out that and then trying to create my migration then lead to a non-empty migration as expected. However, the migration that was created turned the columtype into a nvarchar(255). In fact, I've experimented with different values for [MaxLength(xxx)], and it seems that no matter what I enter, the resulting column type in the migration is nvarchar(255). So my conclusion is that the [MaxLength(xx)] annotation is being ignored.

I am able to modify my DbContext class, in the OnModelCreating method. If I change modelBuilder.Entity<MyEntity>().Property(x => x.MyField).HasMaxLength(100_000); to modelBuilder.Entity<MyEntity>().Property(x => x.MyField).HasMaxLength(10); there, then the resulting migration file looks correct (the column type gets set to nvarchar(10)).

I would like however to be able to specify this in my entity class, using the annotation. Why is it not working?


Update 2:

Looks like I was ruining this for myself with:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.RemovePluralizingTableNameConvention();
            modelBuilder.SetDefaultMaxStringLength(255);
...

So that whatever limit I was trying to impose with MaxLength(x) was being overridden by the modelBuilder.SetDefaultMaxStringLength(255);.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source