Entity Framework: Data Model with column with the largest number of characters possible

Asked

Viewed 350 times

3

I’m creating the tables with the Entity Framework, and I’m using the Data Annotation to determine the amount of characters, I wanted to know which larger size was supported for typing text and whether the correct type would be string even?

In case wanted one that 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; }
  • In the database which is the expected type? text or varchar, alias which bank is using ?

  • Mysql, about the expected type I can’t tell you, from the searches I did, it would be varchar, but when creating the class does not appear. I saw this font http://answall.com/questions/78812/quais-os-typed-existentes-no-mysql-para-textos and this is https://msdn.microsoft.com/pt-br/library/ee382832(v=vs.110). aspx

  • You can wear it in your class string is correct and varchar(255) in Mysql for a text limit, if you do not want text limit use the type text in Mysql.

  • In mysql all right, I understood, but in the class, there is the type text, at least I tried to import (using ctrl + . ), but nothing appeared

  • 1

    the type you represent in the class is string stay calm is correct the way you represented.

1 answer

2

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 the text in the Database when you do not want to limit the size of the text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.