Message: Unable to update the EntitySet – because it has a DefiningQuery and no element exist

Reason: 1) This error often occur due to the absence of primary key in the respective table, or 2)the identity column is not set to true and hence there is no option to set the auto-increment value 3) due to unupdated model file after adding the primary key to the table.

Solutions:
1) This error often occur due to the absence of primary key in the respective table where you are trying to perform an insert operation:

Check your database to see if the table has the primary key is set. If not, add the primary key by “opening the Design view >> right click and set as primary key >>  click save” or by executing the following query.
ALTER TABLE <TABLE_NAME>
ADD CONSTRAINT <CONSTRAINT_NAME> PRIMARY KEY(<COLUMN_NAME>)

Set primary key

Set primary key

2) The identity column is not set to true and hence there is no option to set the auto-increment value:

If the table contains the primary key already, go to the design view and check the column properties of the primary key. If Identity specification is set as No as shown in figure below, change it to Yes and then make sure the auto-increment option below that row is set and the value for the same is 1.

Column Properties

Column Properties

3) Due to unupdated model file after adding the primary key to the table

The third step must be followed if any change is made to the table using above steps, so that your change will be reflected in your Model.

Now, Go to the Visual Studio >> Open your solution with the project having the issue >> Open the Entity Framework Model Designer(.edmx) >> Right click the designer>> Select the model corresponding to the table to click update model from database. Now save and run the solution.

Update Model From Database

Update Model From Database

Hope this helps. Happy Learning!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s