Reputation: 11
I need to save a string with a length larger than 2000 characters, but the Entity Framework validation "hangs" the string size in 2000 and returns me error message:
The Value field must be a string or array type with maximum length of '2000'.
In the database the field is VARCHAR (8000)
and in the entity is defined MaxLength of 8000
Model
public class TermoUso : BaseEntity
{
public Guid TermoUsoKeyId { get; set; }
public virtual TermoUsoKey TermoUsoKey { get; set; }
[Required]
[MaxLength(8000)]
public string Texto { get; set; }
}
DataContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Properties<string>()
.Configure(c => c.HasColumnType("varchar"));
modelBuilder.Properties<string>()
.Configure(c => c.HasMaxLength(8000));
modelBuilder.Properties<DateTime>()
.Configure(c => c.HasColumnType("datetime2"));
}
Upvotes: 1
Views: 3483
Reputation:
Try using 'StringLength':
public class TermoUso : BaseEntity
{
public Guid TermoUsoKeyId { get; set; }
public virtual TermoUsoKey TermoUsoKey { get; set; }
[Required]
[StringLength(8000)]
public string Texto { get; set; }
}
StringLength is a data annotation that will be used for validation of user input.
From MSDN: Specifies the minimum and maximum length of characters that are allowed in a data field
MaxLength is used for the Entity Framework to decide how large to make a string value field when it creates the database.
From MSDN: Specifies the maximum length of array or string data allowed in a property.
Upvotes: 1