Updating your edmx to reflect changes made in your db (.net linq-to-entities)
Asked Answered
L

11

45
  • So I have my edmx made.
  • Then I change my database a little bit, changing a column to from being a NOT NULL to allowing NULL.
  • I go into my edmx, right click and choose "Update Model from Database"

Now I go into my program and it hasnt actually updated... I can't put a null in the column. What do I have to do to update the edmx properly? Thank you.

Lysol answered 27/3, 2009 at 15:34 Comment(0)
M
14

Choosing the Update Model from Database is the best method for updating your EDMX. There are certain properties that don't get updated on the Conceptual layer.

Ensure that your Store layer has been updated by viewing it in the Model Viewer toolbox. If the Store has properly been updated then you're okay, and your database is in sync. If so, go into the visual designer, click the field, go to properties, and update the NotNull property on the Conceptual side.

Maimonides answered 27/3, 2009 at 15:52 Comment(2)
He said in the question that "Update model" didn't work. While informative, your post does not actually solve the problem.Dupont
I agree, when viewed in Model Browser - the model section is updated, but the store section contains old metadata and there is no way to change its properties in there.Sector
J
81

Update/delete from the EDMX is not always functional. If the model doesn't get updated on clicking Update Model from Database let's say when you have updated a view/table in the DB, do the following:

1) Delete the view/table from the model diagram
2) Switch the EDMX to xml view (right click the edmx file and select "Open With")
3) Search and delete the xml entity elements
4) Switch back to EDMX view
5) Click Update Model from Database

This should reflect any kind of change you made to the DB to your EDMX. It's cumbersome, but works flawlessly.

In an ideal world, i would expect the Update Model from Database to sync the changes from DB to EDMX. But, it doesn't work most of the time.

Jeffrey answered 8/10, 2009 at 1:33 Comment(6)
Huagati has a tool to refresh the edmx file, but it's not free. huagati.com/dbmltoolsAdaptable
I had an update to the column size and it did not get refreshed in the data model, so that's not reliable. Best option is to re-add the entity or manually change the properties (like in my case). Thanks for the answer.Twotone
yes you can do it like that but isn't a little bit strange you don't get all updates with update from db? I don't want to add remove entities on each change of schema in db...for me that is pretty inefficient..Mady
"3) Search and delete the xml entity elements" Can someone give a little more detail as to what this step entails?Marshy
Deleting entities for re-adding them is not acceptable as soon as you need to specify things like enum typing on properties or StoreGeneratedPattern, since it causes the loss of those things.Confiscable
I love you! Really, you saved me!Callipash
S
21

What I have successfully done is this (VB.Net).

  1. make updates to the database as desired/ required
  2. Choose "Update from database" in the EDMX model, the graphical model will correctly show the new structure/tables
  3. Expand the project so that it shows all related files
  4. The two files with the "tt" extension are the ones that matter: first take the one WITHOUT the .Context. before the tt extension. right click on it and choose Run Custom Tool:

run custom tool

  1. Do the same for the .tt file with the .Context. in its name. All of your code and logical model classes will be updated.
Soupspoon answered 9/10, 2012 at 12:42 Comment(5)
This is the way I do it as well :), this should be the accepted answerGwenore
This should be the accepted answer.And also please make sure that after "Update from database" you are saving the EDMX modelTorchier
I used to use the "delete and re-add" approach. Today I had to modify some column lengths in a central entity in our large model. Delete and re-add just broke lots of stuff, whereas this approach saved my day. Furthermore, deleted-and-re-added items often become un-versioned.Revel
This works... should be accepted answer. ... Accepted answer did not work in my case.Sanctified
What is run-custom tool? What does it do?Caracara
M
14

Choosing the Update Model from Database is the best method for updating your EDMX. There are certain properties that don't get updated on the Conceptual layer.

Ensure that your Store layer has been updated by viewing it in the Model Viewer toolbox. If the Store has properly been updated then you're okay, and your database is in sync. If so, go into the visual designer, click the field, go to properties, and update the NotNull property on the Conceptual side.

Maimonides answered 27/3, 2009 at 15:52 Comment(2)
He said in the question that "Update model" didn't work. While informative, your post does not actually solve the problem.Dupont
I agree, when viewed in Model Browser - the model section is updated, but the store section contains old metadata and there is no way to change its properties in there.Sector
H
5

Yes, It doesn't work most of the time :-/

The "best method" (because it works systematically) is to delete the EDMX file and generate it again. But don't forget to remove the connection string in App.config (else VS2008 wizzard will add a suffix to the default entity name), and clear the cache.

I hope that these tools will work better in a next release, because it decreases the productivity dramatically...

Hiccup answered 15/12, 2009 at 15:48 Comment(1)
Sadly, In VS 2010 RC2 it still works the same. I agree - its contraproductive and error prone. Deleting EDMX file is the only 100% verified way to make it working.Ietta
D
4

THIS IS THE QUICKEST EASIEST WAY:

  1. Delete the view/table from the .edmx diagram.
  2. NOW use the Update Model from Database to add the table back in.
Didier answered 13/6, 2014 at 21:39 Comment(2)
This doesn't always restore the navigation properties between entitiesMerous
Couldn't agree more. Using EF's "Update Model from database" is as hopeless now, in EF6, as its always been. Delete the table, re-add it, and THEN you'll see any changes to the table structures. Come on Microsoft... get this fixed...Cromagnon
E
4

1.Build the project after updating EDMX file.

2.Right click your .tt file in solution explorer.

3.Select "Run Custom Tool" option.

This will update the .tt file.

Source : here!

Erotomania answered 24/4, 2015 at 11:35 Comment(1)
How is your answer any different from Peter Klein's?Reproach
T
2

Open the edmx file in the VS's XML editor and check to see if there were errors genned when the update was attempted.

  <!--Errors Found During Generation:
      warning 6013: The table/view 'foo.dbo.snafu' does not have a primary key   
      defined and no valid primary key could be inferred. This table/view has  
      been excluded. To use the entity you will need to review your schema,  
      add the correct keys and uncomment it.

  <EntityType Name="snafu">
    <Property Name="snafu_column" Type="smallint" />
  </EntityType>-->

In the above case...Adding a primary key to the table in question caused the "Update Model from Database" to work.

Trillium answered 14/1, 2010 at 19:1 Comment(0)
M
2

This answer is better: https://mcmap.net/q/245048/-how-do-you-update-an-edmx-file-with-database-changes

Any manual editing is ripe for errors or will be lost when someone uses any tool like the wizard. Deleting and updating with the wizard is much better.

Monopolist answered 2/10, 2014 at 16:46 Comment(0)
E
1

It doesnt update for example max length for string attributes!

If you work with TFS it is not good to delete the file, you want to keep the history and not affect others.

For me it works having a tiniy separate project that I can use to completely recreate the edmx file, I open it in xml, copy paste to the existing one, and move one shape in the model in order for VS to recreate the .cs file. Voila, it is now updated.

Ethnomusicology answered 27/10, 2010 at 20:9 Comment(3)
There are third party tools that can keep it in sync, including maxlength, precision/scale, default values, identity/computed etc. One tool is my add-in for VS2010. If you want to see it in action, watch the screencast videos in these two blog posts: bit.ly/cTbreP bit.ly/cCbnrn You can download the addin and get a trial license at huagati.com/dbmltools if you want to try it out...Gefen
I've noticed with VS2013, TFS -- that if I work in the design, and tweak an entity, it deletes the .CS backing file and re-creates it all over again. This effectively removes it from TFS, and I hve to keep re-adding the dang things.Inutile
Sledgehammer workaround for the delete thing: switch to git-tfs and say goodbye to most of your tfs woes. git-tfs.comBludge
R
1

A view I created in the database was not appearing in the designer (after choosing "Update model from database..." and adding a check next to the name of the view). I saw no error message until I switched the EDMX to xml view:

  • Right click the edmx file
  • Select "Open With..."
  • Select "Automatic Editor Selector (XML)"
  • Click Find and search for your view name

In the edmx xml I found:

"Errors Found During Generation: warning 6013: The table/view '(view name)' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it."

I modified the view to have a primary key. Then I opened the edmx designer and ran "Update model from database..." and the view then appeared in the designer as expected with no errors.

Receptacle answered 25/7, 2018 at 13:17 Comment(0)
C
0

Removing all tables from designer view and Updating after that works for me

Coquito answered 19/9, 2017 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.