.NET Core - Migration Fluent API HasColumnType .NET Core - Migration Fluent API HasColumnType postgresql postgresql

.NET Core - Migration Fluent API HasColumnType


The default ColumnType of string will automatically map to the Max length field. so be leaving off the column type you should get what you want.

builder.Property(x => x.Foo)


I dont know the approach you are using if its Database first or Code first approach

  • For Database First: You can simply run the below script in the package manager console and making sure to have set the default project where you want the import to be

For MSSQL

Scaffold-DbContext "Server=xxxxxx;Database=TestDatabase;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -F -OutputDir Models

The above will generate virtually all the properties of the tables


For Postgres

 Scaffold-DbContext "{YourConnectionString}" Npgsql.EntityFrameworkCore.PostgreSQL -F -OutputDir Models

Let me know if this helps.


As previously stated, using the default should give you what you want, i.e. max length for your text column.

builder.Property(x => x.Foo)

However, if you want to be explicit and not use the default values, you may use something like this:

builder.Property(x => x.Foo).HasColumnType("nvarchar(max)");

If you only want to change the maximum length, this api is also available:

builder.Property(x => x.Foo).HasMaxLength(50);