alanh alanh - 1 month ago 19
C# Question

Max length not applied in migration

I am using efcore.sqlserver 1.0.1 with preview2 tools - I have a string property which in sql server is created as an nvchar(max) field. When I add a data annotation stringlength(100) to the property and add a new migration the migration does not alter the column at all.

However if I add a Required and StringLength annotation then the generated migration alters the column and shows ( .. maxLength: 100, nullable: false )

Why does it only do this if I change the nullable value?

Answer

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.

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