The type of the key field ‘JobId’ is expected to be ‘System.Int32’, but the value provided is actually of type ‘System.Int64’.

less than 1 minute read

It's a Easter Monday, very quiet everywhere. I have been working with Sqlite and had an weird error. When I try to update a record in sqlite, using Entity Framework, it complains that the type of primary key id is system.int64.

My code is below

JobTable jobToUpdate = (from j in _entities.JobTableSet
where j.JobId == job.Id
select j).FirstOrDefault();

jobToUpdate.Author = job.Author;
jobToUpdate.Customer = job.Customer;
jobToUpdate.Date = job.Date;
jobToUpdate.Description = job.Description;
jobToUpdate.Title = job.Title;

_entities.ApplyPropertyChanges(jobToUpdate.EntityKey.EntitySetName, jobToUpdate);

I struggled to sort it out for 30 minutes and gave up. I have a similar code in ASP.Net MVC 1 and it works ok.


30 minutes later.

For some reason, integer column in sqlite is Int64. When you use Entity Framework and create classes from database using the automated tool, it converts the column to Int32. So you have to change it to Int64 manually. Secondly, if you use your own model class, the type of the key needs to be long, not int, as int translates into Int32 (not sure what happens in 64bit windows)