我如何更新一个Linq到SQL .dbml文件?
当前回答
有三种方法可以使模型保持同步。
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.
DBML设计器在Visual Studio 2015、2017或2019中默认不安装。你必须关闭VS,启动VS安装程序并修改你的安装。LINQ to SQL工具是必须安装的特性。对于VS 2017/2019,你可以在单独组件>代码工具下找到它。
其他回答
更新表和更新DBML之间有细微差别…如果对现有表进行了更改,外键关系并不总是立即引入。解决方法是构建项目,然后重新添加表。我向微软报告了这个问题,在VS2010中得到了修复。
DBML显示不显示新的外键约束
请注意,主答案中给出的说明并不清楚。更新表
打开dbml设计面 选择所有带“右”的表->单击->选择全部或CTRLa CTRLx(削减) CTRLv(粘贴) 保存并重新构建解决方案。
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.
在存储过程更新的情况下,应该从.dbml文件中删除它,然后重新插入它。但是如果存储过程有两条路径(例如:if something;显示一些列;否则显示一些其他列),确保两个路径有相同的列别名!!否则只有第一个路径列存在。
我建议使用VS2008内置的可视化设计器,因为更新dbml也会更新为您生成的代码。在可视化设计器之外修改dbml将导致底层代码不同步。
有三种方法可以使模型保持同步。
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.
DBML设计器在Visual Studio 2015、2017或2019中默认不安装。你必须关闭VS,启动VS安装程序并修改你的安装。LINQ to SQL工具是必须安装的特性。对于VS 2017/2019,你可以在单独组件>代码工具下找到它。