Entity Framework – Unable to update the EntitySet … because it has a DefiningQuery …

Running into very special issues with some kind of new programming techniques is … well, just that: special. I had one of those very special issues some minutes ago and had to learn some more detailed facts about the Entity Framework in .NET.

The situation:
I’m working with a data model created using the import function of Visual Studios Entity Framework model designer. The database model is physically created directly in the database using database management tools and the model in Visual Studio is just being updated. Everything seems to be okay for me after having added two new tables to the database structure, but I should have looked at the output window, which was stating „Das Modell wurde mit Warnungen oder Fehlern generiert.“ (should be something like „The model has been generated with warnings or errors“, idk the concrete message in English, sorry). Did not recognize that, so I worked on implementing the usage of my new tables.

The issue:
When using the new tables I ran into a runtime exception (System.Data.UpdateException) stating the following error message: „Unable to update the EntitySet ‚[NameOfTheEntitySet]‘ because it has a DefiningQuery and no element exists in the element to support the current operation.“
Okay, mh, I never saw that message before, so it becomes strange and special at the same time for me.

The reason / solution:
There has been another warning after importing the database structure into the data model which was stating the reason for my error: In one of the tables I forgot to set a primary key constraint, so the Entity Framework imported the database, generated a key combination on its own and made the table kind of readonly. This is why I was not able to do insert, update or delete queries on the table. Very important: You will receive that message too, if you are trying to state change queries on views in your model.
The solution was very simple. Adding the primary key constraint to the structure, reimport into the model and everything worked fine. You should note, that the Entity Framework will not remove its automatically created key combination with your new primary key constraint, so you have to do that in your model on your own.

GD Star Rating
loading...

Ähnliche Einträge:

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *


*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>