Max length not applied in migration
I believe you need to use MaxLengthAttribute
instead of StringLengthAttribute
.
https://docs.efproject.net/en/latest/modeling/max-length.html#data-annotations
This is probably because the StringLength
attribute has an option for minimum length, which is not supported natively by SQL, and therefore the MaxLength
attribute is a better fit for this use case.
Just to be clear, attributes have no effect in and of themselves. They can contain logic and information, but must be utilized via reflection from another piece of code during the normal execution model. This is with the exception of certain attributes which are given special meaning by the compiler, such as the Conditional
attribute.
EDIT
The author found that this is a known issue for cases when the tool set has been upgraded from RC2 => RTM.
https://github.com/aspnet/Announcements/issues/195
Fortunately there is no such issue on the EF core :) I have tested your scenario and it is working fine.
You have to use [MaxLength(100)]
attribute for that.Here is the doc : MaxLength Data Annotations
Test case : I used MaxLength as 500 for my test.
Firstly I have created a property like this :
public string Title { get; set; }
After migration :
After that I have changed it like this :
[MaxLength(500)]
public string Title { get; set; }
After migration :
Generated Script :
migrationBuilder.AlterColumn<string>(
name: "Title",
table: "Posts",
maxLength: 500,
nullable: true);
Tested Tools version :
<package id="Microsoft.EntityFrameworkCore.Tools"
version="1.0.0-preview2-final" targetFramework="net461"
developmentDependency="true" />