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)?
Thank you for the reply,
HasColumnTypeit became clear to me the explanation, nowHasMaxLength: define o tamanho máximo de caracteres de um campowhere exactly this limitation (52 characters) is applied?– rubStackOverflow
@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.
– novic
Okay, with this explanation the answer is complete. I’m also testing at Visual Studio.
– rubStackOverflow
@rubStackOverflow I did test, and what intrigued me the most is that the field length settings are ignored by the database
SQLite, who actually accepts2147483647which 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 fieldvarchar(255). Well I’ll leave it that way until I find more information, but, that’s how it works.– novic
But with regard to
HasMaxLengthwhere exactly is the validation applied? It would be in thefrontend? It would be interesting an example, even simple, of the use.– rubStackOverflow
In the case of Sqlite nowhere. @rubStackOverflow, you have to use the Viewmodel technique
– novic
I get it, I’m going to do some tests since I’m going to use MVVM in the test I’m doing.
– rubStackOverflow
Any doubt we are there and any discovery I post also @rubStackOverflow
– novic