用于特定表的 SQL Metal 或刷新/将表添加到 .dbml 文件的其他方式

发布于 2024-09-10 16:39:45 字数 172 浏览 3 评论 0原文

任何人都可以在 Visual Studio 中轻松执行此操作,而无需使用服务器资源管理器?

我也尝试查看宏,但记录只产生

  Sub TemporaryMacro()
  End Sub

所以没有运气。

有什么办法编写这个脚本吗?

anybody have any easy way of doing this in Visual Studio, without having to use the Server Explorer ?

I tried also looking at macro's but recording only produce

  Sub TemporaryMacro()
  End Sub

So no luck there.

Any way to script this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后eg是否自 2024-09-17 16:39:45

我正在使用与此类似的批处理脚本来在基础表/视图更改时管理模型的更新。要使用它:

  1. 让 SQLMetal 和 SQLMetalInclude (您可能必须从源代码下载并构建)可从当前目录访问目录(或在路径中)
  2. 创建一个文件“chosenEntities.txt”,每行包含表/视图名称。这些表将是为其生成代码的唯一表。
  3. 修改、保存并执行Generate.bat批处理代码
  4. (可选)将.bat文件作为外部运行工具添加到Visual Studio,如下所述此处

生成.bat:

sqlmetal /conn:"Data Source={Hostname};Initial Catalog={DBName};User ID={Username};Password={Password}" /dbml:temp.dbml /views

setlocal EnableDelayedExpansion
set file=chosenEntities.txt
set include=
FOR /F %%i IN (%file%) DO (
set include=!include!%%i,
)
set include=%include:~0,-1%

sqlmetalinclude -dbml:temp.dbml -output:ChosenEntities.dbml -include:%include%

sqlmetal /context:CustomDataContext /pluralize /namespace:MyNamespace.DB /language:csharp /code:DBEntities.cs /entitybase:DBEntityBase ChosenEntities.dbml

selectedEntities.txt 示例:

Accounts
Customers
PRODUCTS_VIEW
AnotherTable

说明:

  1. 中间 .dbml 文件由 sqlmetal 照常生成。创建的 dbml 文件包含数据库中每个表的定义(以及视图,如果选择的话)。
  2. 下一个代码块循环遍历 selectedEntities.txt,并为每一行附加行内容(表名称)加上逗号 ',
  3. '最后一个尾随逗号被删除
  4. 运行 sqlmetalinclude,这基本上会生成原始 temp.dbml 文件的副本,但仅包括您指定的表。还有其他可用选项,例如
  5. 第二次运行 sqlmetal 时转换各个表名称,这次根据缩减的 .dbml 文件生成实际的 .cs 代码。

I am using a batch script similar to this to manage the updating of my models when the underlying tables/views change. To use it:

  1. Have SQLMetal and SQLMetalInclude (you may have to download and build from source) accessible from the current directory (or in the path)
  2. Create a file "chosenEntities.txt" with a table/view name on each line. These tables will be the only ones that code is generated for.
  3. Modify, save and execute the Generate.bat batch code
  4. (Optional) Add the .bat file as an external run tool to Visual Studio, as explained here

Generate.bat:

sqlmetal /conn:"Data Source={Hostname};Initial Catalog={DBName};User ID={Username};Password={Password}" /dbml:temp.dbml /views

setlocal EnableDelayedExpansion
set file=chosenEntities.txt
set include=
FOR /F %%i IN (%file%) DO (
set include=!include!%%i,
)
set include=%include:~0,-1%

sqlmetalinclude -dbml:temp.dbml -output:ChosenEntities.dbml -include:%include%

sqlmetal /context:CustomDataContext /pluralize /namespace:MyNamespace.DB /language:csharp /code:DBEntities.cs /entitybase:DBEntityBase ChosenEntities.dbml

chosenEntities.txt example:

Accounts
Customers
PRODUCTS_VIEW
AnotherTable

Explanation:

  1. Intermediate .dbml file is generated by sqlmetal as usual. The created dbml file contains the definitions for every single table in the database (and views, if selected)
  2. The next code block loops through chosenEntities.txt, and for each line appends the line contents (table name) plus a comma ','
  3. The last trailing comma is removed
  4. Run sqlmetalinclude, which basically makes a copy of the original temp.dbml file, but only including the Tables you specified. There are other options available, like transforming the individual table names
  5. sqlmetal is run a second time, this time generating the actual .cs code based on the cut-down .dbml file.
剑心龙吟 2024-09-17 16:39:45

SqlMetal Include 对我来说就像一个魅力。
首先使用 SqlMeta 创建一个完整的 dbml 文件 - 说 testComplete.dbml

现在提供此文件作为 SqlMetaInclude 的输入
SqlMetalIninclude /dbml:"testComplete.dbml" /output:"testSubSet.dbml" /include:dbo.SampleTable1=SampleTable1,dbo.SampleTable2=SampleTable2

请注意,此工具包含一个可以处理完整过程的 GUI。

SqlMetal Include worked like a charm for me.
First Create a complete dbml file using SqlMeta - Say testComplete.dbml

Now provide this file as an input to SqlMetaInclude
SqlMetalInclude /dbml:"testComplete.dbml" /output:"testSubSet.dbml" /include:dbo.SampleTable1=SampleTable1,dbo.SampleTable2=SampleTable2

Note that this tool in has a GUI included which can handle the complete process.

橘虞初梦 2024-09-17 16:39:45

有一个很好的实用程序可以帮助您从数据库更新现有的 DBML 文件:Huagati DBML/EDMX 工具< /a>.

它不是免费的,但对于任何严肃的 Linq-to-SQL 开发来说都值得投资。

唯一的选择是自己编写 - 读取数据库结构并将其与 DBML 中的 XML 表示进行比较,并根据需要更新 DBML。

There is one good utility out there which helps you update your existing DBML files from the database: Huagati DBML/EDMX tools.

It's not free, but worth the investment for any serious Linq-to-SQL development.

The only alternative would be to write it yourself - read the database structure and compare that to the XML representation in the DBML, and update the DBML as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文