Search code examples
c#entity-frameworkmappingstoreef-database-first

Entity Framework maps wrong datatype


I have a database with a view that has an integer FK. When I create my model in VS and look at the diagram the view has the column as a string/varchar. I have removed the table and re-added it but it still creates the entity with a string property.

If I select the view then over in Properties change Type to Int32 the build completes without errors, but breaks at the first call of the DB context saying the store and model don't match up.

DB table:

TariffKey : int
CompanyKey : int
ChargeTypeKey : int
ChargeTypeName : varchar(80)
Value : numeric
StartDate : datetime
EndDate : datetime

Model mapping:

TariffKey : int (Int32)
CompanyKey : int (Int32)
ChargeTypeKey : varchar (String)
ChargeTypeName : varchar (String)
Value : numeric (Decimal)
StartDate : datetime (DateTime)
EndDate : datetime (DateTime)

Solution

  • The Store of the Model was the problem child. So even when I changed the data type from the diagram the store still had a varchar mapped to the field. I followed Mat's answer from Update Model from Database does not see a type change