Migrations HasMaxLength x HasColumnType

1

I'm creating a simple test table with EF Core e SQlite

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

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

Migration snippet created by model builder 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 snippet created by model builder above

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

The result of the two tests is reflected differently in the creation of the database, the first one creates a field correctly varchar(51) and the second one creates a default field Text

I was in doubt.

It was not for the two to create the same type field varchar ?

Does it make any difference between these two properties ( HasMaxLength e HasColumnType )?

    
asked by anonymous 17.09.2016 / 04:52

1 answer

1
  

Could not both create the same field type varchar ?

No, it is for it to create the default type SQLite that is Text . When the responsibility is passed to the bank generation it puts the default type that can be, depending on the data type of 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 encoding (UTF-8, UTF-16BE or UTF-16LE).
  • BLOB. The value is a blob of data, stored exactly as it was input.

Within the Text type 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 for varchar(51) the bank accepted the setting, because is allowed . / p>

  

Does it make any difference between these two properties ( HasMaxLength and HasColumnType )?

Yes, they are methods that define different settings :

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

These two settings are not exactly rendered to SQLite in Text (or variations) fields that have information on the site : The current implementation will only support the string or BLOB length up to 2 31 -1 or 2147483647 . Validation must be done with ViewModel decorating with DataAnnotations [MaxLength(52)] , summarizing for SQLite the mapping settings have no validity in the specific case quoted, different for SQLServer , MySQL and PostGreSQL serving as restriction and information returning errors for application .

  

Conclusion

Normally set this way:

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

This is the default for text fields in SQLite (Text) and if you want restrict / validate with DataAnnotations to have the necessary control on the models

Links:

17.09.2016 / 17:47