How do I update a Linq to SQL .dbml 开发者_运维百科file?
There are three ways to keep the model in sync.
Delete the modified tables from the designer, and drag them back onto the designer surface from the Database Explorer. I have found that, for this to work reliably, you have to:
a. Refresh the database schema in the Database Explorer (right-click, refresh)
b. Save the designer after deleting the tables
c. Save again after dragging the tables back.Note though that if you have modified any properties (for instance, turning off the child property of an association), this will obviously lose those modifications — you'll have to make them again.
Use SQLMetal to regenerate the schema from your database. I have seen a number of blog posts that show how to script this.
Make changes directly in the Properties pane of the DBML. This works for simple changes, like allowing nulls on a field.
The DBML designer is not installed by default in Visual Studio 2015, 2017 or 2019. You will have to close VS, start the VS installer and modify your installation. The LINQ to SQL tools is the feature you must install. For VS 2017/2019, you can find it under Individual Components > Code Tools.
To update a table in your .dbml-diagram with, for example, added columns, do this:
- Update your SQL Server Explorer window.
- Drag the "new" version of your table into the .dbml-diagram (report1 in the picture below).
- Mark the added columns in the new version of the table, press Ctrl+C to copy the added columns.
- Click the "old" version of your table and press Ctrl+V to paste the added columns into the already present version of the table.
- Delete the dragged table from step 2 and save the .dbml file.
You could also check out the PLINQO set of code generation templates, based on CodeSmith, which allow you to do a lot of neat things for and with Linq-to-SQL:
- generate one file per class (instead of a single, huge file)
- update your model as needed
- many more features
Check out the PLINQO site at http://www.plinqo.com and have a look at the intro videos.
The second tool I know of are the Huagati DBML/EDMX tools, which allow update of DBML (Linq-to-SQL) and EDMX (Entity Framework) mapping files, and more (like naming conventions etc.).
Marc
We use a custom written T4 template that dynamically queries the information_schema model for each table in all of our .DBML files, and then overwrites parts of the .DBML file with fresh schema info from the database. I highly recommend implementing a solution like this - it has saved me oodles of time, and unlike deleting and re-adding your tables to your model you get to keep your associations. With this solution, you'll get compile-time errors when your schema changes. You want to make sure that you're using a version control system though, because diffing is really handy. This is a great solution that works well if you're developing with a DB schema first approach. Of course, I can't share my company's code so you're on your own for writing this yourself. But if you know some Linq-to-XML and can go to school on this project, you can get to where you want to be.
I would recommend using the visual designer built into VS2008, as updating the dbml also updates the code that is generated for you. Modifying the dbml outside of the visual designer would result in the underlying code being out of sync.
There is a nuance to updating tables then updating the DBML... Foreign key relationships are not immediately always brought over if changes are made to existing tables. The work around is to do a build of the project and then re-add the tables again. I reported this to MS and its being fixed for VS2010.
DBML display does not show new foreign key constraints
Note that the instructions given in the main answer are not clear. To update the table
- Open up the dbml design surface
- Select all tables with Right->Click->Select All or CTRLa
- CTRLx (Cut)
- CTRLv (Paste)
- Save and rebuild solution.
In the case of stored procedure update, you should delete it from the .dbml file and reinsert it again. But if the stored procedure have two paths (ex: if something; display some columns; else display some other columns), make sure the two paths have the same columns aliases!!! Otherwise only the first path columns will exist.
Here is the complete step-by-step method that worked for me in order to update the LINQ to SQL dbml and associated files to include a new column that I added to one of the database tables.
You need to make the changes to your design surface as suggested by other above; however, you need to do some extra steps. These are the complete steps:
Drag your updated table from Server Explorer onto the design surface
Copy the new column from this "new" table to the "old" table (see M463 answer for details on this step)
Delete the "new" table that you just dragged over
Click and highlight the stored procedure, then delete it
Drag the new stored procedure and drop into place.
Delete the .designer.vb file in the code-behind of the .dbml (if you do not delete this, your code-behind containing the schema will not update even if you rebuild and the new table field will not be included)
Clean and Rebuild the solution (this will rebuild the .designer.vb file to include all the new changes!).
Use Visual Studio 2019 open DBML files in graphic designer to update DBML files.
when not working:https://developercommunity.visualstudio.com/t/dbml-files-not-opening-in-graphic-designer-in-vs-2/1659675
Just for the record
精彩评论