Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change of MaxLength not migrated to DB #3434

Closed
mehrt opened this issue Oct 15, 2015 · 2 comments
Closed

Change of MaxLength not migrated to DB #3434

mehrt opened this issue Oct 15, 2015 · 2 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mehrt
Copy link

mehrt commented Oct 15, 2015

I'm using beta8. So I don't think this is a dupe of #882, but maybe a followup on it.
I followed the steps given here.

If I do ...

modelBuilder.Entity<Blog>()
    .Property(b => b.Url)
    .IsRequired()
    .HasMaxLength(256);

... and run the initial migration, I get a Blog table with a column defined as Url (nvarchar(256), not null, as I would expect.
If I subsequently change this to ...

modelBuilder.Entity<Blog>()
    .Property(b => b.Url)
    .IsRequired()
    .HasMaxLength(512);

... and do Add-Migration / Update-Database, the field stays as it was in the db, even though the change in HasMaxLength is present in the ModelSnapshot class:

b.Property<string>("Url")
    .IsRequired()
    .Annotation("MaxLength", 512);

If the only thing I change for the second migration is the value for HasMaxLength as done above, I get the following Migration code:

public partial class MySecondMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
    }
    protected override void Down(MigrationBuilder migrationBuilder)
    {
    }
}

Which probably is the reasony why the database isn't changed. If I manually change this to ...

public partial class MySecondMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<string>("Url", "Blog", "nvarchar(512)");
    }
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<string>("Url", "Blog", "nvarchar(256)");
    }
}

... applying and reverting the second migration works as expected.
I assume it's a bug that a change of MaxLength in the model is not migrated to the database? Because if I remove the .IsRequired() from the model, the column is altered as it should be if I run the migration, so subsequent column changes actually do seem to work. If I modify two properties at once (ie. change IsRequired() and HasMaxLength), only the change of the first one is migrated to the db.

@sthossan
Copy link

sthossan commented Feb 2, 2023

I face this problem when I use EF Core code first. I change my migration code entity.Property(t => t.UserName).HasColumnType("varchar")..HasMaxLength(256) to entity.Property(t => t.UserName).HasColumnType("varchar(256)") and now it's working fine

@SalmanAerovative
Copy link

After redesigning my domain to not require a string with large length, i found that entity framework was not honoring my HasMaxLength() because I had configured it twice in two different locations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

6 participants