I'm doing some testing with Entity Framework by mapping and migrating a database from a legacy system.
Scenery
Where I have a table similar to this:
TESTE
--------------------------------------
ID INTEGER NOT NULL,
NOME VARCHAR(100),
ATIVO SMALLINT DEFAULT 0 NOT NULL
--------------------------------------
And in my model this:
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 I run a simple query in this table, I'm 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'
.
What I understand is because I am using long
( System.Int64
) in the template and my database the column is of type INTEGER
(which it considers System.Int32
). This does not seem to make much sense, since System.Int32
can easily be set to a System.Int64
. (The opposite would not be enough).
I know that:
Id
attribute in my template to int
( System.Int32
) will work; ID
to BIGINT
(which it considers System.Int64
) will also work; But these two options would not be viable for my case, as:
Id
attribute of type long
is in class
BaseModel
which is a system convention, and is long
because new tables are being created with ID
BIGINT
. To outline this the model could not inherit from BaseModel
, which would not be the best option. BIGINT
, since it has contraints
and PKs
attached to it which makes the job difficult. Question