How to set fixed-length columns in EF Core?

In a previous EF, I could do this:

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

This will lead to migration with something like

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

However, there is no EF Core IsFixedLength().

Is there any other way to do this?

+6
source share
2 answers

Currently (EF Core 1.1.0), you can directly specify the type of column: .HasColumnType("char(123)")

Please make sure your database engine understands this value! It is transmitted “as is”.

Also note that here you must write all the necessary measurement / length / accuracy values, as the value .HasMaxLength()will be ignored.

+10

, 2.1, :

entity.Property(e => e.Name).HasMaxLength(X).IsFixedLength();
0

Source: https://habr.com/ru/post/1661816/


All Articles