find a location for property in a new city

Tuesday 27 July 2010

The specified value is not an instance of type 'Edm.Int64' Parameter name: value. When inserting an object with Entity Framework

When trying to insert an object into my entity data model (i.e. myObjectContext.SaveChanges()) using Entity Framework 4, I got the most confusing error that said "The specified value is not an instance of type 'Edm.Int64' Parameter name: value".

What made it more confusing was that another repository that uses the same entity data model inserted fine. What made it even more confusing was that if I went back to the page that displays the inserted items they were in fact there no matter how many times I refreshed. What made it even more confusing was that they weren't actually in the database!

Mysterious solution

Beware: you won't leave this blog feeling enlightened. What I did worked but made no sense but fixed it:

Given that there is clearly some sort of caching happening I decided to refresh the Application pool for my application. This cleared out all those phantom items that existed on the page but not in the database. Cool...

Then I tried to insert a new item again for a laugh. It worked! It seemed as though recycling the app pools (AKA giving the server a kick up the arse) suddenly made it start working again.

Step 2: Back away slowly from the code and hope it doesn't happen again.

Update: Concrete solution

The problem was I am using Spring.NET for dependency injection and by default it is set to make the references singletons. So I had to change
<object name="MyRepository" type="MyAssembly.MyNamespace.MyRepository, MyAssembly"/>
<object name="MyRepository" type="MyAssembly.MyNamespace.MyRepository, MyAssembly" singleton="false"/>
and it all now works as expected.

Follow britishdev on Twitter

No comments:

Post a Comment