Entity Framework Validation confusion - maximum string length of '128'

For Entity Framework 4.3.1, 5.0.0 and 6.2.0, you can use IsMaxLength().

Property(m => m.Body ).IsRequired().IsMaxLength();

Configures the column to allow the maximum length supported by the database provider.

https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.modelconfiguration.configuration.stringcolumnconfiguration.ismaxlength?view=entity-framework-6.2.0


If you get this error using a Model First approach, check the EF Model: it may be simply that a property on the Entity you're updating has the Max Length attribute set.


Default length of string field in code first is 128. If you are using EF validation it will throw exception. You can extend the size by using:

[StringLength(Int32.MaxValue)]
public string Body { get; set; }

This post became somehow popular so I'm adding second approach which also works:

[MaxLength]
public string Body { get; set; }

StringLengthAttribute is from System.ComponentModel.DataAnnotations assembly and MaxLengthAttribute is from EntityFramework assembly (EF 4.1).


May be you have used

Property(m => m.Body ).IsRequired().HasMaxLength(128);

On your dbcontext class in OnModelCreating. So change as per your length