How do you update edmx file with database changes?

I have an edmx file and I changed the table in my database. I know that there is a wizard called “Update Model from Database”, but in many cases this is useless.

For example, if I change the field from non-null to nullable, or if I delete the fields, the update model does not reflect the changes. I had to delete the object and add it back so that the changes appear in my model.

On the following question: How to propagate database changes in my .edmx file?

One answer seems to say the same thing that you need to delete the object and add it back.

Is this the final answer or is there a better way to do this?

+28
entity-framework edmx edmx-designer
Mar 14 '12 at 18:45
source share
4 answers

An important first step is to understand what happens when you use the update model wizard.

From the MSDN Library :

The Entity Designer (Entity Designer) entity data model constructor uses the update wizard to update the .edmx file from changes made to the database. The upgrade model wizard overwrites the warehouse model as part of this process. The update model wizard also makes some changes to the conceptual model and mappings, but only makes these changes when objects are added to the database. For example, new entity types are added to the conceptual model when tables are added to the database, and new properties are added to entity types when columns are added to the table. For more information about what changes are made to the .edmx file, see Changes Made to. The edmx file using the update model wizard .

When you updated the database using the update model wizard, it updated the storage model in the .edmx file, not the conceptual model. When changes to the definition of existing objects occur, only the warehouse model is updated; The conceptual model is not updated. For a complete description of the changes made by the update model wizard, see the section "Changes Made to the .edmx File Using the Update Wizard" above.

Below are some options for updating objects that are not updated by the update model wizard (based on your scenario in which the column definition was changed):

  • Use the update model wizard (to update the warehouse model), open the .edmx file using the constructor (by default), find the desired scalar property and edit the necessary properties in the property windows.
  • Use the update model wizard (to update the warehouse model), open the .edmx file with the XML editor, find the desired property in the CSDL (conceptual model) section and change the necessary attributes. This is basically the same as option 1, but you are editing the XML directly (finding and replacing can be useful here).
  • In the Model Explorer, delete the desired object from the "Object Types" section of the conceptual model and the desired table from the "Tables / Views" section of the storage model. Then use the update model wizard to add it back.

The best option will depend on this scenario. For example, if you just changed the definition of a single column, then option 1 is likely to be the best choice. If you changed the definition of the number of columns in one table, then option 3 might be your best bet. If you have changed the column that is used in many tables (for example, primary / foreign key), perhaps the best option would be to edit the .edmx XML file.

+27
Mar 14 2018-12-12T00:
source share
— -

Updating EDMX in a safe way:

As you have already found, updating from a database does not always correctly modify existing properties.

From our daily use of EDMX updates (100 seconds of updates over 24 months), I would recommend the following sequence for updating EDMX.

Delete existing model and then update:

  • Open EDMX Designer
  • Ctrl-A to select all
  • Delete key to delete all models in the designer
  • IMPORTANT: do not save EDMX at this moment if you are under source control! *
  • Now right-click and select "Update Model from Database" to re-create the entire model.
  • Reconstruction of the project for distribution of changes

This will obviously lose any manual settings that you have made to the model, but manual settings should be avoided whenever possible. This makes the whole process reproducible at any time (which is good).

Important notes:

  • If you have automatic saving in Visual Studio, you need to quickly select the update (step 5 above) to avoid automatic saving, saving everything.
  • If you are under source control and save EDMX after that, your source will mark all generated files as “deleted”, and updating EDMX again may disable files that are not in the original control !.
  • This process will not update any stored procedures. In addition, I found that updating EDMX will also not update stored procedures in which only the return type has changed (still current as EF 6.1.1).

Additional recommendation:

Keep EDMX in a separate library. It also becomes a great place to add additional TT files and partial classes (for example, to extend the functionality of EDMX models). I also post any extension methods for the database context in this library. The migration files are generated in the library, as a result of which all this is perfectly preserved.

April 2015 update

The latest version 4 of Visual Studio 2013 seems to have resolved many problems with TFS. Now we will see the created Visual Studio files, and then return them if they have not changed. The above steps still seem the most secure.

September 2015 update

Using the latest version of VS2013 Release 5, we still have problems if saving happens during EDMX update. You may still be in a state where pending deletes cause your tt files to be deleted from the original control during the update. The secret is to update quickly between steps 4 and 5! :)

+47
May 27 '14 at 9:39
source share

If I understand your question and your example, as soon as you make the update model from the database step, and you sit there on the Model.edmx diagram, you can highlight the property in the class that you want to change and show the properties on it and change the property Nullable for it Nullable: True. This is at least one way to do this.

I believe that the idea here is that the conceptual model (which does not change from non-zero to zero) may actually differ from the base table of the database, and therefore it does not change this part, and this difference may be exactly what you intend . The two ways I do this is to either remove and add, as you mentioned, or, more typically, I manually set the properties, as I mentioned.

+1
Mar 14 2018-12-12T00:
source share

We believe that I have added a new column (c1) to the existing table. Then, to update the same in my existing entity model, I would do the following.

I will open the .edmx file in notepad ++.

I will add the c1 property to the .edmx file where necessary. For example, I would add c1 node below each c0 node.

  <EntityType Name="table"> <Key> <PropertyRef Name="Id" /> </Key> <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> <Property Name="c0" Type="nvarchar(max)" /> <Property Name="c1" Type="nvarchar(max)" /> </EntityType> 

Reload the project in Visual Studio.

Finally, add the c1 attribute to the model.

+1
Nov 23 '16 at 15:37
source share



All Articles