C# 在数据库架构更改后将 LINQ 更新为 SQL 类的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:47:56  来源:igfitidea点击:

Best way to update LINQ to SQL classes after database schema change

提问by Compile This

I'm using LINQ to SQL classes in a project where the database design is still in a bit of flux.

我正在一个项目中使用 LINQ to SQL 类,该项目的数据库设计仍处于不稳定状态。

Is there an easy way of synchronising the classes with the schema, or do I need to manually update the classes if a table design changes?

是否有一种简单的方法可以将类与模式同步,或者如果表设计发生变化,我是否需要手动更新类?

采纳答案by vzczc

You can use SQLMetal.exe to generate your dbml and or cs/vb file. Use a pre-build script to start it and target the directory where your datacontext project belongs.

您可以使用 SQLMetal.exe 生成 dbml 和/或 cs/vb 文件。使用预构建脚本启动它并定位到您的 datacontext 项目所属的目录。

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sqlmetal.exe 
  /server:<SERVER> 
  /database:<database> 
  /code:"path\Solution\DataContextProject\dbContext.cs" 
  /language:csharp 
  /namespace:<your namespace>

回答by Michael Haren

I think Jeff complained about this recently. One common technique is to drag all the objects into the designer again...

我想杰夫最近抱怨过这个。一种常见的技术是再次将所有对象拖到设计器中......

I hope someone else chimes in with a better approach!

我希望其他人能提出更好的方法!

回答by Espo

I haven't tried it myself, but Huagati DBML/EDMX Toolsis recommended by other people.

我自己没试过,但是其他人推荐的Huagati DBML/EDMX Tools

Huagati DBML/EDMX Tools is an add-in for Visual Studio that adds functionality to the Linq2SQL/DBML diagram designer in Visual Studio 2008, and to the ADO.NET Entity Framework designer in Visual Studio 2008 SP1. The add-in adds new menu options for updating Linq2SQL designer diagrams with database changes, for renaming Linq-to-SQL (DBML) and EF (EDMX) classes and properties to use .net naming conventions, and for adding documentation/descriptions to Linq-to-SQL generated classes from the database properties.

Huagati DBML/EDMX 工具是 Visual Studio 的一个插件,它为 Visual Studio 2008 中的 Linq2SQL/DBML 图设计器和 Visual Studio 2008 SP1 中的 ADO.NET 实体框架设计器添加了功能。该加载项添加了新的菜单选项,用于通过数据库更改更新 Linq2SQL 设计器图表、重命名 Linq-to-SQL (DBML) 和 EF (EDMX) 类和属性以使用 .net 命名约定,以及向 Linq 添加文档/描述-to-SQL 从数据库属性生成的类。

Screenshot of DBML Tools

DBML 工具的屏幕截图

回答by Rory Becker

DamienG has written some t4 templateswhich can replace some of what VS generates for you. These can be rerun whenever you like via a command line tool.

DamienG 编写了一些t4 模板,可以替换 VS 为您生成的一些模板。这些可以通过命令行工具随时重新运行。

T4 templates have the added benefit of being editable. This allows you to tweak what is generated to you hearts content.

T4 模板具有可编辑的额外好处。这使您可以调整生成的内容。

回答by mcintyre321

I wrote a tool to do script changes to Dbml scripts see http://code.google.com/p/linqtodbmlrunner/and my blog http://www.adverseconditionals.com

我编写了一个工具来对 Dbml 脚本进行脚本更改,请参阅http://code.google.com/p/linqtodbmlrunner/和我的博客http://www.adverseconditionals.com

回答by Levite

Here is an easy fix without any additional software, that just works for simple changes (like added fields, few tables, etc).

这是一个无需任何其他软件的简单修复,仅适用于简单的更改(如添加的字段、几个表等)。

Instructions:

指示:

  • You pull a copy of the changed table into the designer (will be removed later)
  • Now select all the new (or changed) fields and (right-click ->) copy
  • In your original table right click and insertthem (delete changed fields first)
  • Now delete the table you copied them from
  • 您将更改后的表的副本拉入设计器(稍后将被删除)
  • 现在选择所有新的(或更改的)字段和 ( right-click ->)copy
  • 在您的原始表格中右键单击insert它们(首先删除更改的字段)
  • 现在删除您从中复制它们的表

I know it is kinda obvious, but somehow non-intuitive, and it helped me a lot, since all the right attributes and types will be copied, and all links stay intact. Hope it helps.

我知道这有点明显,但不知何故不直观,它对我帮助很大,因为将复制所有正确的属性和类型,并且所有链接保持不变。希望能帮助到你。

When to use:

何时使用:

Of course it is - as said - for small changes, but surely better than manually replacing tables with many links, or when you don't want your whole database structure generated by SQLMetal. For example when you have a big amount of tables (e.g. SAP), or when using cross-linked tables from different databases.

当然 - 如上所述 - 对于小的更改,但肯定比手动替换具有许多链接的表要好,或者当您不希望由 SQLMetal 生成整个数据库结构时。例如,当您有大量表(例如 SAP)时,或使用来自不同数据库的交叉链接表时。

回答by Dean

How about modifying the Properties of the entity/table within the DataContext design surface within Visual Studio?

如何在 Visual Studio 的 DataContext 设计图面上修改实体/表的属性?

For instance if I added a column to an SQL Server table:

例如,如果我向 SQL Server 表中添加了一列:

  1. Open the *.dbml file.
  2. Right click the entity and select Add > Property.
  3. Fill out the values in the Properties window for the new column.
  4. Build your solution.
  1. 打开 *.dbml 文件。
  2. 右键单击实体并选择添加 > 属性。
  3. 在新列的“属性”窗口中填写值。
  4. 构建您的解决方案。

The auto generated model classes should reflect the new column that was added.

自动生成的模型类应该反映添加的新列。

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明