Monday, 19 August 2013

Entity Framework Error, Saying there is no Primary Key, when there is one

Entity Framework Error, Saying there is no Primary Key, when there is one

I had a table containing some very unreliable data, due to this it had no
PK. I am in the process of cleaning the data up and allowing for it to be
added to. However I am getting an error
Message "Can't perform Create, Update, or Delete operations on
'Table(TableName) because it has no primary key"
The error is thrown when trying to Insert to the table.
The database set up is using SSDT and Database First. The Database is
managed in a VS2012 project. After updating the database project with a
PK, I also updated the edmx. I had to remove a generated Entity Key
created by EF so the new added PK was the only one on the table.
The key now shows correctly in the Model. As the error was still happening
I created a partial class for the Entity, added a metadata annotation and
create the relevant metadata class. I then added the entity property and
decorated it with [Column(IsPrimaryKey=true)].
I am still getting the error and I am out of ideas of things to try. If I
breakpoint just before insert and look at the context, there is a
IsReadOnly property set to true, which may be relevent.

No comments:

Post a Comment