EF - Generate with No Table Key
One thing to keep an eye out when using the Entity Framework - Generate From DB option is how tables with no keys are handled. When one of these tables is encountered the Entity is mapped to a Defining Query instead of the physical table in the store.
The reason you care is you are not able to update / modify an item mapped to Defining Query. Currently, the designer doesn't give an indication that it's a read-only entity - it would be great if in the future it could visually show it in the designer.
There are a few clue's to tell this has occurred, first, you Entity will have every eligible column marked as part of the Entity Key(basically all non nullable / non binary) . You can also view the markup for the SSDL, and you will see the Defining Query when you look at that table in the store markup. The most obvious indication this exists if you try to update it you will get the following error "Unable to update the EntitySet 'Events' because it has a DefiningQuery and no <DeleteFunction> element exists in the <ModificationFunctionMapping> element to support the current operation".
When the Generate From DB runs, it also creates an informational message to tell you this has happened. It says something like " The table/view 'DemoTable' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view. "
On a new DB, this can easily happen during the initial stages if you add the table to the EDM before the real key is added. To fix, you can add the proper key to the database then update the model from the database again to pickup the change.
After the update, you will have to manually correct the entity key's on the Entity as currently in V1, it only updates the definition in the store, and not the conceptual definition.
As indicated in the error above, you can also map to stored procedures and leave it a Defining Query. That is typically done when you intended to use a Defining Query and not when it happened just because there's no key.
I think it would be great if the Generate From Db wizard would give you warnings before the generate was completed. This would be especially important when you are updating an existing model if there was any potential problem that could be avoided.
References (1)
-
Response: Links of the Week July 25thThis week I am coming to you from the Microsoft Campus. So as you would expect I have a lot of energy
Reader Comments