Up Create the entity data model
Model » Create the entity data model » Update Model from Database

Update Model from Database

Last modified on August 15, 2012 17:22

When the database changes, your Entity Data Model (EDM) must change to match. This topic shows how to update the model from the database using the Entity Framework Update Model Wizard.


You probably shouldn't model every table in the database all at once. You'll add new entities over time and, if you use the Data-First approach, you'll want to initialize those entities and their mappings from information in the database just as you did when you first created the model with the EDM Wizard

The application is not static. New requirements arrive all the time and they often involve changes to the database some of which you must incorporate into your model.

Please don't start over

That can be tempting. But if you do, you'll lose all of the changes you made along the way. These changes are in your EDMX file and there is no easy way to find and merge them into a new EDM.

Instead you use the Entity Framework Update Model Wizard to add new entities to your model and to update previously mapped entities when their corresponding database objects have changed.

Here we only touch upon the issues that are of most concern to the DevForce EDM developer. You can learn more about the Update Model Wizard from Microsoft's documentation on MSDN.

Using the Wizard

You can update your model anytime. Right-click on the canvas and select Update Model from Database.... This launches the Entity Framework Update Model Wizard as seen here.

Always, always, always backup the EDMX before you update the model from the database.

EdmUpdateModelWizardAdd.png

  1. Select the tables to model as entities. The wizard only shows tables (and views) that are not already modeled in your EDM. You can only model a database object once.

  2. Keep Pluralize or singularize generated object names checked if you want to continue using the English language pluralization rules; these are the same rules used by the EDM Wizard.

  3. Keep Include foreign key columns in the model checked. All associations in a DevForce EDM must be "Foreign Key Associations".

When the wizard closes:

  • New entities appear for the tables and views that you added.
  • New properties appear for columns added to tables that are already entities in your model.
  • New properties appear if columns were renamed. The entity property mapped to the old column remains in the model

Beware of delete and rename

The Update Model Wizard never deletes and never renames anything in your conceptual entity model

The wizard only adds new items and removes mappings that it no longer understands.

If you drop the "Customer" table from the database and it was formerly mapped to the Customer entity, the Customer entity remains in your model ... where it is now unmapped because the wizard can't find the matching database table.

If you renamed the "Customer" table to "Company", the "Company" table appears in the list of objects you can model. The wizard doesn't know that you already modeled that table under its old name. 

When you select the "Company" table in the wizard, the wizard adds a new Company entity to your model. It also preserves the Customer entity which is unmapped because the wizard can't find the matching table.

The same dynamic applies to properties. If the "CompanyName" column was deleted, Customer.CompanyName remains although it is no longer mapped. If the column was renamed to "CustomerName", there will be a new property, Customer.CustomerName that is mapped to that column. The old Customer.CompanyName survives as an unmapped property as well; you'll have to delete it manually.

Be very careful when deleting or renaming primary and foreign keys columns. The wizard doesn't understand what you are doing. Existing properties and associations will become unmapped and new key properties will appear. 

An entity that once had a single part key will have a composite two part key:  the old key property under the old name and the new key property under the new name property. This change cascades to the associations that depend upon that key.

You will have to make manual repairs when you delete or rename anything in the database.

The model will not validate until you repair the model and the entity classes generated from the model will be incorrect until you do. Pay attention to the EDM error messages. They can be a little confusing. You can power through it if you know what changed in the database and remember that the wizard only adds, never deletes.

We recommend that you modify the database incrementally, making a small change and then updating the model immediately. This is particularly important when renaming or deleting database objects that you have previously mapped to your EDM.

Storage and mapping sections are regenerated

The wizard's guiding principal is that the conceptual entity model is yours. That's why it only adds to the conceptual entity model; it won't remove or change anything in the conceptual model.

However, the wizard acts as if it owns the storage and mapping schemas. You should assume that the wizard rewrites everything in the mapping and storage schemas. Of course it reads your previous mappings and preserves them if it understands them. But if it erases anything it doesn't recognize or that conflicts with its reading of the database schema.

You cannot control this behavior. You cannot tell the wizard "refresh these entities but leave those alone." The wizard reevaluates your entire model ... and rewrites the storage and mapping sections as it sees fit.

This fact is especially relevant if you have made manual changes to the storage or mapping sections of the XML. Pay particular attention to the section "Entity Framework Features Not Supported by the Entity Designer" in the Microsoft documentation of the EDM Designer

You can work around those limitations by editing the XML directly to implement features the designer doesn't support. Just remember that the wizard probably will erase those changes. You will have to restore them from your backup of the edmx file.   

Tags: Model EDM
Created by DevForce on March 21, 2011 14:48

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2020 IdeaBlade