Reputation: 24182
This thing is driving me crazy, and the error is quite meaningless to me:
Unable to update the EntitySet 'TableB' because it has a DefiningQuery and no element exists in the element to support the current operation.
My tables are put like this:
TableA int idA (identity, primary key) ... TableB int idA (FK for TableA.idA) int val
TableB has no defined primary key in the SQL server. The Entity Framework has imported the table and the association and set both fields as key. But it will output that error when I try to do an insert into the table!
What's wrong??
Edit: As suggested by Alex, the solution was this:
I left the key as it was, since it was OK to me that both the columns are part of the key.
Upvotes: 124
Views: 71524
Reputation: 20924
Well when a table is encountered without a PrimaryKey it is treated as a View.
And views show up in the EDMX file (open in an XML editor to see) in the StorageModel\EntitySet[n]\DefiningQuery element.
When you have a DefiningQuery the Entity becomes readonly unless you add modification functions. You need 3 modifications functions (aka Stored Procedures) one for each of Insert, Update and Delete.
But you have two options:
Change the key definion:
In your case I recommend (1).
Upvotes: 142
Reputation: 2986
Hope it will solve the problem.
Upvotes: 1
Reputation:
Add primary key to table, delete the model from the edmx model, then select update from database, build and run...... works
Upvotes: 5
Reputation: 180
@Palantir. Verify that both of you tables have Primary Keys set, and be careful with multiple primary keys set in a table.
Upvotes: 1
Reputation: 902
I was missing a primary key on my table and got this error message. One thing I noted was after I added the key to the table, I needed to clear the table from the edmx using the designer, save the edmx, then update it again to add the table back in. It wasn't picking up the key since it was already assigned as a view. This didn't require editing the edmx manually.
Upvotes: 6
Reputation: 1271
Just Add a primary key to the table. That's it. Problem solved.
ALTER TABLE <TABLE_NAME>
ADD CONSTRAINT <CONSTRAINT_NAME> PRIMARY KEY(<COLUMN_NAME>)
Upvotes: 13