3

In Entity Framework 6.X It was possible to change the default database type by doing this:

modelBuilder.Properties<string>().Configure(c => c.HasColumnType("varchar"));

In EF Core how can i do that?

The method Properties() doesn´t exists.

Marcos Lommez
  • 126
  • 10
  • 3
    Possible duplicate of [Loop/reflect through all properties in all EF Models to set Column Type](https://stackoverflow.com/questions/41468722/loop-reflect-through-all-properties-in-all-ef-models-to-set-column-type) – Ivan Stoev Sep 15 '17 at 23:54

1 Answers1

0

Since EF Core 6, based on github repository of entity framework (see here) :

previous versions of EF Core (before EF Core 6) require that the mapping for every property of a given type is configured explicitly when that mapping differs from the default. This includes "facets" like the maximum length of strings and decimal precision, as well as value conversion for the property type.

In the class that inherits from DbContext override ConfigureConventions

protected override void ConfigureConventions(
    ModelConfigurationBuilder configurationBuilder)
{
    configurationBuilder
        .Properties<string>()
        .AreUnicode(false)
        .HaveMaxLength(1024);
}

Thus all string properties can be configured to be ANSI (instead of Unicode) and have a maximum length of 1024, hence mapping string to nvarchar changes to varchar when migrations are applied to the database.

mahooresorkh
  • 1,361
  • 2
  • 8
  • 16