leora
leora

Reputation: 196429

Add column to the database - how do I update the dbml file?

Is there a way I can refresh the dbml file or do I have to delete it and generate a whole new one?

Upvotes: 26

Views: 20016

Answers (7)

Jason Geiger
Jason Geiger

Reputation: 2112

Note: If you cannot open the DBML file in the graphical interface, add the "LINQ to SQL Tools" mentioned here https://superuser.com/questions/1193196/how-to-open-dbml-in-designer-mode-with-vs

To expand the answer for the modern Visual Studio 2022 environment, I had to...

  1. Close the Solution
  2. Open the DBML as a text file and add the new field. Save and close the file
  3. Open the solution
  4. Open the DBML in the visual GUI editor by double clicking it. You need the LINQ to SQL tools mentioned above
  5. Click on the table in question and add a new property, the name is of it is not important, "Property1" will suffice
  6. Save and rebuild.
  7. Delete the new temporary field from #5
  8. Save and rebuild, everything works.

Upvotes: 0

JamesG
JamesG

Reputation: 388

So far as I know there's no built-in functionality in the designer to update a table with changes in a database.

There are third party tools (http://www.huagati.com/dbmltools/) that offer that sort of functionality.

Whenever I ran into this issue it was on the order of one or two columns on a table with multiple manual relationships already set up, so I just edited the XML (right click on the DBML and select open with -> XML) and added the columns to the table definition.

When DBML file is saved the associated .designer.cs file is automatically updated with code for newly added columns. Therefore, there is no need to drop and recreate table in designer mode.

Upvotes: 23

user1228
user1228

Reputation:

I've heard of one weird trick where you create a new entity for your modified table and then drag/cut the new property from the new entity to your old one. Never tried it, however.

Upvotes: 17

ezekiel571
ezekiel571

Reputation: 11

I couldnt delete my table and re-add (this is the quickest way) since its used as a return type somewhere. Also its probably a bad idea if your db is really complex

Best way for me was this:

Add Same Table to dbml Open dbml with xml edit (right click -> open with) Copy XML column section from table you just added and paste in column section of offending table. Save Open Dbml regular way Delete added table

Upvotes: 1

Jim Wooley
Jim Wooley

Reputation: 10398

If you have made modifications to your model, droping the table and re-adding it may cause more problems than it's worth. In those cases, I would recommend manually modifying the designer (or dbml file). Don't touch the .Designer file as those changes will be overwritten.

Another alternative to consider is to use SqlMetal as part of your build process to regenerate the model from the database.

Upvotes: 0

Kevin Jones
Kevin Jones

Reputation: 2367

There's no mechanism to automatically refresh the DBML or to tell the DBML to re-read the database.

I believe the easiest way is to open the dbml file in the designer, delete the table and then re-add it to the design surface. Although this may depend on the complexity of the database, i.e. you may lose certain information.

I'd backup the dbml file first before trying :)

Upvotes: 2

Kieran Senior
Kieran Senior

Reputation: 18220

It's usually quite a pain. Recompile, shut down VS, open it back up etc. It should work, but for me there was always a delay. It may be easier to manually create your model using LINQ though which is what I do. That way I've got full control over it.

Upvotes: 1

Related Questions