asp.net-coreentity-framework-coreef-core-7.0

Why does MaxLength(10) revert to varchar(1) in Entity Framework Core 7?


I am trying to set MaxLength (ASP.NET Core, EF Core 7) for a string property on my model.

I can apply the migration with no problem at all. However, once the database created after running 'dnx ef database update', the model that I have set the MaxLength(10) for has data type varchar(1) in database.

I have checked the DBContext as well as the snapshot file, and the column has max length 10.

I am not sure why the code first is not picking up the MaxLength.


Solution

  • It turned out that I had Data Annotation on both the models and the dbcontext. It has to be on only one of them.

    I have removed all Data Annotations like IsRequired() and HasMaxLength from dbcontext, 'onModelCreating' method and only have them on my models. I have updated the database for InitialSchema migration and all the datatypes are now correct.