entity-frameworkef-code-firstentity-framework-coreentity-framework-migrationsef-fluent-api

How do I configure fixed length columns in EF Core?


In the previous EF, I could do this:

  entityTypeBuilder
    .Property(b => b.Foo)
    .IsRequired()
    .HasMaxLength(10)
    .IsFixedLength();

That would generate a migration with something like

Foo = c.String(nullable: false, maxLength: 10, fixedLength: true)

However in EF Core there is no IsFixedLength().

Is there some other way of doing this?


Solution

  • Currently (EF Core 1.1.0) you can only specify column type directly: .HasColumnType("char(123)")

    Please make sure your DB engine understand this value! It is passed "as is".

    Also please note that you should write all required dimension/length/precision values here, because .HasMaxLength() value will be ignored.