Set EF6 Code First strings fluently to nvarchar(max)

I don't know if you've found an answer by now, but in case anyone else is wondering how to do it, simply set the SQL datatype and ignore the HasMaxLength() invocation.

        modelBuilder.Properties<string>()
            .Where(p => p.CustomAttributes.Any(a => typeof(TextAttribute).IsAssignableFrom(a.AttributeType)))
            .Configure(p => p.HasColumnType("nvarchar(max)"));

Using IsMaxLength() or HasMaxLength(null) would set the field to nvarchar(4000) (varchar(8000) if specifying the data type as varchar).


If you're doing it inside an EntityTypeConfiguration<MyEntity> class, it is similar to @RickNo's answer, but is done like this:

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

There is a method to indicate that you use the maximum allowed by the database.

IsMaxLength ()

    modelBuilder.Properties<string>()
        .Where(p => p.CustomAttributes.Any(a => typeof(TextAttribute).IsAssignableFrom(a.AttributeType)))
        .Configure(p => p.HasColumnType("nvarchar").IsMaxLength());