Friday, January 8, 2010

EF error: Unable to update the EntitySet 'tablename' because it has a DefiningQuery and no

I got a strange Entity Framework error today at runtime. I was trying to insert a new entity/row into the database and I got the following error:

"Unable to update the EntitySet '[tablename]' because it has a DefiningQuery and no element exists in the element to support the current operation."

It turns out the table did not have a primary key defined. Whoops. Because of this, EF did something a little different. It basically treated the table as a view and it seemed to use a combination of all the foreign keys in the table as a composite key, I think.

The solution was to define the primary key for the table and then update the model. This fixed most of the problem, but it also introduced a compile time error about the model. It turned out I then had to go and manually mark the foreign key fields as not part of the primary key.

1 comment:

Dorababu said...

How can I over come this error with out having a primary key in the table.