16

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?

Dejan
  • 9,150
  • 8
  • 69
  • 117
grokky
  • 8,537
  • 20
  • 62
  • 96

4 Answers4

18

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.

Dmitry
  • 16,110
  • 4
  • 61
  • 73
  • 2
    That is a pity, EF Core does not seem "complete". Also, [according to docs](http://learn.microsoft.com/en-us/ef/core/modeling/max-length) there is a max length... – grokky Nov 25 '16 at 05:22
  • EF Core is not a full replacement to E6, there are a lot of [issues on github](https://github.com/aspnet/EntityFramework/issues). As a workaround, you may create own extensions method (`IsFixedLength(length)`) which calls `HasColumnType($"char({length})")` and control all this magic strings from one place. – Dmitry Nov 25 '16 at 15:43
  • 1
    About `HasMaxLength` - yes, it exists and working ok. But if you apply `HasColumnType("char(123)")` and `HasMaxLength(100)` to same property/column - 123 "wins". – Dmitry Nov 25 '16 at 15:46
  • I'm dealing with old code and have a problem with MS SQL timing out. I noticed that I had a problem with MS SQL / C# where it was using varchar(8000) instead of varchar(1). The code was using HasColumnType("char"), and one would assume this to be a single character, but it isn't. If you use HasColumnType("char"), it will default to varchar(8000) on the DB side. char is fixed-size in bytes that can be between 1-8000, so it uses varchar(8000). Thanks for the answer. This saved me a lot of time. – adprocas Jan 15 '21 at 16:26
  • In case anyone is interested, this link explains the char vs. varchar side a bit more - https://learn.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-ver15 – adprocas Jan 15 '21 at 16:28
  • very very thanks. HasMaxLength ignore when I defined (e=>e.ICC).HasColumnType("varchar").HasMaxLength(30) and on Db set varchar(1) .But when defined (e=e.ICC).HasColumnType("varchar(30)").HasMaxLength(30) set varchar(30) on DB. EFCore3.1 – Omid Rahimi Sep 26 '21 at 12:22
17

Actually, as for Version 2.1 this is implemented as:

entity.Property(e => e.Name).HasMaxLength(X).IsFixedLength();
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
  • Has this been removed? I Can't find this. – Martijn Feb 25 '21 at 14:41
  • 3
    @Martijn make sure you have Microsoft.EntityFrameworkCore.Relational somewhere in your dependency graph as the IsFixedLength() function is an extension method not in the main Microsoft.EntityFrameworkCore assembly. – Buvy Aug 19 '21 at 22:23
3

For anyone coming to this issue like I did earlier today if you are looking for the IsFixedLength() function it is an extension method on the RelationalPropertyBuilderExtensions class in the Microsoft.EntityFrameworkCore.Relational package

Buvy
  • 1,174
  • 12
  • 16
0

For those looking for a more recent example, in efcore version 6 you can implement an IEntityTypeConfiguration, and include the registration in your setup as follows;

public class MyEntityConfiguration : IEntityTypeConfiguration<MyEntity>
{

    public void Configure(EntityTypeBuilder<MyEntity> builder)
    {
        builder.Property(o => o.MyProperty).HasMaxLength(2000);
    }
}

And include the registration on your DbContext class;

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());//Your assembly here
        base.OnModelCreating(builder);
    }

I personally prefer this method because you separate out the concerns into different files, rather than having a monolithic DbContext. This answer does not preclude the other answers, it's just an additional option.

Dan Rayson
  • 1,315
  • 1
  • 14
  • 37