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

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);
_entities.SaveChanges();

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)

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s