I\'ve been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max).
This is what I
Update for @Slauma answer.
Using a override for all strings like this in OnModelCreating
:
modelBuilder.Properties().Configure(s =>
s.HasMaxLength(256).HasColumnType("nvarchar"));
and then modifying properties with attributes like this:
[Column(TypeName = "nvarchar(MAX)")]
public string CaseComment { get; set; }
Or this:
modelBuilder.Entity()
.Property(b => b.CaseComment)
.HasColumnType("nvarchar(MAX)");
This can cause the Exception. Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Even though the column is of correct data type Entity Framework still thinks it is nvarchar(256)
and throws the error DbEntityValidationException
.
To fix this use the following instead:
[Column(TypeName = "nvarchar(MAX)")]
[MaxLength]
public string CaseComment { get; set; }
Or
modelBuilder.Entity()
.Property(b => b.CaseComment)
.HasColumnType("nvarchar(MAX)")
.HasMaxLength(null);