c# – Entity Framework: Data Model with column with as many characters as possible

Question:

I'm creating the tables with the Entity Framework , and I'm using Data Annotation to determine the amount of characters, I wanted to know what is the largest supported size for typing texts and if the correct type would be string ?

In case I wanted one that would fit as many as possible. By default use with 255.

[DisplayName("Informações Diversas")]
[Required(ErrorMessage = "Preencha as informações diversas")]
[StringLength(255, MinimumLength = 3, ErrorMessage = "As informações diversas deve ter de 3 a 255 caracteres")]
public string InformacoesDiversas{ get; set; }

Answer:

You can use System.ComponentModel.DataAnnotations.Schema.ColumnAttribute to define that your property will be created as text in the Database.

[Column(TypeName = "text")]
public string InformacoesDiversas { get; set; }

Or through Fluent API:

modelBuilder.Entity<OTipoDaSuaEntidadeAqui>()
    .Property(e => e.InformacoesDiversas)
    .HasColumnType("text");

It is recommended to use text in the Database when you don't want to limit the text size.

Scroll to Top