Tuesday, June 3, 2014

Breeze, Angular and Entity Framework 'AutoGeneratedKeyType'

I am building an application with Breeze, Angular, WebAPI2 and Entity Framework. It is a big learning curve, but I am making progress.

This issue had me stumped for a little while. When I tried to create a new entity with the createEntity method in Breeze, I was getting this error:

“Cannot attach an object to an EntityManager without first setting its key or setting its entityType 'AutoGeneratedKeyType' property to something other than 'None'”

Googling suggested that it was because the metadata had not been fetched. However, this turned out not to be the problem.

What actually happened was that I used the database first approach to generate the EDMX in Visual Studio. At one point the ID of my table was not an Identity column. I fixed this and refreshed the EDMX from the database.

What I had not realised was that refreshing did not set the StoreGeneratedPattern property to Identity.


Without that set the metadata returned by the breeze controller was this:


With that set the metadata becomes:


Then I was able to create the entity without the error.

No comments: