Migrations Hasmaxlength x Hascolumntype

Asked

Viewed 616 times

1

I am creating a simple test table with EF Core e SQlite

Follow the first test in which the modelbuilder creates a migration (shown just below)

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Curso>()
        .Property(c => c.Nome)
        .HasColumnType("varchar(51)");
}

Migration excerpt created by the Builder model above

Nome = table.Column<string>(type: "varchar(51)", nullable: true),

Second test:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Curso>()
        .Property(c => c.Nome)
        .HasMaxLength(52);
}

Migration excerpt created by the Builder model above

Nome = table.Column<string>(maxLength: 52, nullable: true),

The result of the two tests are reflected differently in the creation of the dataset, the first creates a field correctly varchar(51) the second creates a standard field Text

I was in doubt.

Wasn’t the two of you supposed to create the same kind of field varchar?

Is there any difference between these two properties (HasMaxLength e HasColumnType)?

1 answer

1


Wasn’t the two of you supposed to create the same kind of field varchar?

No, it is for him to create the type set by default by the bank SQLite that is Text. When responsibility is passed on to the bank generation it sets the standard type that they can be, depending on the data type of the classe:

  • NULL. The value is a NULL value.
  • INTEGER. The value is a Signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes Depending on the magnitude of the value.
  • REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
  • TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
  • BLOB. The value is a blob of data, stored Exactly as it was input.

Within the type Text there are variations (cast) that are considered Text:

  • CHARACTER(20)
  • VARCHAR(255)
  • VARYING CHARACTER(255)
  • NCHAR(55)
  • NATIVE CHARACTER(70)
  • NVARCHAR(100)
  • TEXT
  • CLOB

Reference: Affinity Name Examples

That’s why when you set up varchar(51) the bank accepted the configuration because, is permitted.

Is there any difference between these two properties (HasMaxLength and HasColumnType)?

Yes, these are defining methods different configurations:

  • HasMaxLength: sets the maximum character size of a field.
  • HasColumnType: defines the type of data that will be accepted or stored in a given field of your table.

These two settings are not exactly processed for SQLite in fields Text (or variations) that has as information on the website: The Current implementation will only support a string or BLOB length up to 2 31-1 or 2147483647. The validation shall be done with ViewModel decorating with Dataannotations, [MaxLength(52)], in short, to SQLite the mapping settings have no validity at all in the specific case cited, different to SQLServer, MySQL and PostGreSQL which serves as restriction and returning information mistakes of bank for applying.

Completion

Set normally this way:

modelBuilder.Entity<Cliente>()
           .Property(c => c.Nome)
           .ForSqliteHasColumnName("nome")                                            
           .IsRequired();

this being the standard for text fields in SQLite (Text) and if by chance you want restrict/validate brood ViewModel with Dataannotations to have the necessary control on models

Links:

  • 1

    Thank you for the reply, HasColumnType it became clear to me the explanation, now HasMaxLength: define o tamanho máximo de caracteres de um campo where exactly this limitation (52 characters) is applied?

  • 1

    @rubStackOverflow should be applied only in validation, because, if you do not specify (this only happens in Sqlite) the type it puts Text. Let’s do the following I will test the validation, and until tomorrow I put the test... Everything in the answer was done code and testing in Visual Studio. vlw.

  • Okay, with this explanation the answer is complete. I’m also testing at Visual Studio.

  • 1

    @rubStackOverflow I did test, and what intrigued me the most is that the field length settings are ignored by the database SQLite, who actually accepts 2147483647 which is a long length so maybe it never happens. Different in other banks that if this is not configured, example, Sqlserver if you do not put the size it creates a field varchar(255). Well I’ll leave it that way until I find more information, but, that’s how it works.

  • But with regard to HasMaxLength where exactly is the validation applied? It would be in the frontend? It would be interesting an example, even simple, of the use.

  • 1

    In the case of Sqlite nowhere. @rubStackOverflow, you have to use the Viewmodel technique

  • 1

    I get it, I’m going to do some tests since I’m going to use MVVM in the test I’m doing.

  • 1

    Any doubt we are there and any discovery I post also @rubStackOverflow

Show 3 more comments

Browser other questions tagged

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