1
I am doing some tests with Entity Framework mapping and migrating a database of a legacy system.
Setting
Where I have a table similar to that:
TESTE
--------------------------------------
ID INTEGER NOT NULL,
NOME VARCHAR(100),
ATIVO SMALLINT DEFAULT 0 NOT NULL
--------------------------------------
And in my model that:
public class Teste: BaseModel
{
public virtual string Nome { get; set; }
public virtual bool Ativo { get; set; }
}
public class BaseModel
{
public virtual long Id { get; set; }
}
Since my Fluent mapping would be this:
var testeMapper = modelBuilder.Entity<Teste>().ToTable("TESTE");
testeMapper.HasKey(x => x.Id).Property(x => x.Id).HasColumnName("ID");
testeMapper.Property(x => x.Nome).HasColumnName("NOME");
testeMapper.Property(x => x.Ativo).HasColumnName("ATIVO");
Problem
When executing a simple query in this table, I am surprised by the following error:
The type of the key field 'Id' is expected to be 'System.Int64', but the value provided is actually of type 'System.Int32'
.
Which, from what I understand, is why I’m using long
(System.Int64
) in the template and my database the column is of the type INTEGER
(that he considers System.Int32
). Which doesn’t seem to make much sense, because the System.Int32
can easily be set in a System.Int64
(The other way around).
I know that:
- If you change the attribute type
Id
on my model forint
(System.Int32
) will work; - If you change the column type
ID
forBIGINT
(that he considersSystem.Int64
) will also work;
But these two options would not be viable in my case, because:
- The Attribute
Id
of the kindlong
is in theclass
BaseModel
which is a system convention, and islong
because the new tables are being created withID
BIGINT
. For this outline the model could not inherit fromBaseModel
, which would not be the best option. - The ID column would give a great upkeep to be converted to
BIGINT
, that’s rightcontraints
andPKs
linked to it which makes the work difficult.
Question
- Is there any way to turn off this Entityframework check?
- Or is there any configuration that allows the model and table to continue this way?
Sure I understood your alternative and I’m already implementing here in my tests, and it seems to work accordingly. Just one more question, as I understood the functioning of this check it does a query in the table metadata and checks if the types are matching. And can’t this be disabled for production? To avoid overhead in the query? I believe this only occurs when starting Context, but even so it seems unnecessary for production environment!
– Fernando Leal
It uses corresponding patterns and types between database and classes, at the time of the Insert it sends the type and errors can occur in the translation. Until version 6.1.3 it works like this, particular of the ORM. NEVER seen setting to disable, nowhere, I honestly think it does not have. I will generate an Insert Log, and add in the answer, as soon as you enter the pc I am in the cell.
– novic
@Fernando I put the two logs one with Insert of the ID type
int
and the otherLong
. it’s time for theInsert, Update e Delete
he sends vestments to the bank that has types and then the problem can occur if it is not the respective one. In fact, the Firebird installed is the one who makes this generation of SQL.– novic