使用 SqlMetal 和 Visual Studio 时自动生成 DataContext 设计器文件

发布于 2024-07-05 22:49:36 字数 959 浏览 6 评论 0原文

我正在使用 SqlMetal 来为我的 ASP 通用 DataContext.dbml 类。 NET 应用程序使用 LinqToSql。 当我最初创建 DataContext.dbml 文件时,Visual Studio 使用它来创建相关的 DataContext.designer.cs 文件。 此设计器文件包含 C# 中的 DataContext 类,该类在整个应用程序中使用(并且从 dbml 文件中的 XML 派生),对于弥合 SqlMetal 的输出和通过 LinqToSql 使用 DataContext 之间的差距至关重要。

但是,当我对数据库进行更改并重新创建 dbml 文件时,设计器文件永远不会在我的网站中重新生成。 相反,会保留旧的设计器文件(因此无法通过 LinqToSql DataContext 类访问对 DBML 文件的任何更改)。

到目前为止,我能够用来重新生成设计器文件的唯一过程是

  1. 转到Windows 资源管理器并删除dbml 和designer.cs 文件
  2. 转到Visual Studio 并在解决方案资源管理器中单击“刷新”。 dbml 和 Designer.cs 文件现在从项目中消失。
  3. 使用 SqlMetal 重新生成 dbml 文件
  4. 转到 Visual Studio 并在解决方案资源管理器中单击“刷新”。 现在,designer.cs 文件已重新创建。

似乎 Visual Studio 仅在检测到尚不具有 Designer.cs 文件的新 dbml 文件时才会生成 Designer.cs 文件。 这个过程非常不切实际,因为它涉及几个手动步骤,并且会弄乱源代码控制。

有谁知道如何自动重新生成 Designer.cs 文件,而不必遵循上面概述的手动删除/刷新/重新生成/删除过程?

I am using SqlMetal to general my DataContext.dbml class for my ASP.net application using LinqToSql. When I initially created the DataContext.dbml file, Visual Studio used this to create a related DataContext.designer.cs file. This designer file contains the DataContext class in C# that is used throughout the app (and is derived from the XML in the dbml file) and is essential to bridging the gap between the output of SqlMetal and using the DataContext with LinqToSql.

However, when I make a change to the database and recreate the dbml file, the designer file never gets regenerated in my website. Instead, the old designer file is maintained (and therefore none of the changes to the DBML file are accessible through the LinqToSql DataContext class).

The only process I have been able to use so far to regenerate the designer file is

  1. Go to Windows Explorer and delete both the dbml and designer.cs files
  2. Go to Visual Studio and hit Refresh in the Solution Explorer. The dbml and designer.cs files now disappear from the project.
  3. Regenerate the dbml file using SqlMetal
  4. Go to Visual Studio and hit Refresh in the Solution Explorer. Now the designer.cs file is recreated.

It seems that Visual Studio will only generate the designer.cs file when a new dbml file is detected that does not yet have a designer.cs file. This process is pretty impractical, since it involves several manual steps and messes things up with source control.

Does anyone know how I can get the designer.cs file automatically regenerated without having to follow the manual delete/refresh/regenerate/delete process outlined above?

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

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

发布评论

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

评论(2

寄人书 2024-07-12 22:49:36

当您在 Visual Studio 中更改 DBML 时,designer.cs 文件通常会自动维护。 如果重新创建 DBML 时 VS 没有运行,它可能不知道。

检查 Visual Studio 中的 .DBML 文件是否将“自定义工具”属性设置为 MSLinqToSQLGenerator。 如果不是,则将其设置为该值。 如果是,请尝试在进行更改后右键单击 DBML,然后选择“运行自定义工具”以查看是否会更新 .designer.cs。

您还可以使用 SqlMetal 生成类文件:

sqlmetal /code:DataContext.designer.cs /language:csharp DataContext.dbml

The designer.cs file is normally maintained automatically as you make changes to the DBML within Visual Studio. If VS isn't running when you recreate the DBML it may not know.

Check that the .DBML file in Visual Studio has Custom Tool property set to MSLinqToSQLGenerator. If it isn't, then set it to that. If it is try right-clicking on the DBML after making changes and choosing Run Custom Tool to see if that updates the .designer.cs.

You can also generate the class file using SqlMetal:

sqlmetal /code:DataContext.designer.cs /language:csharp DataContext.dbml
逆蝶 2024-07-12 22:49:36

不知道它是如何做到的,但我做了一些事情来让它恢复。

有东西锁定了它,因此它生成了一个新的 db.designer.cs 文件 (db1.designer.cs)。

我打开了超越比较,将该文件与前一个文件进行比较(BC 不应该锁定,我不认为这是问题所在,以前从未遇到过这个问题。)

在记事本中打开项目文件并查找这些条目,我在源代码管理中恢复到以前的版本..

这就是我带回来的。

<Compile Include="db.designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>db.dbml</DependentUpon>
</Compile>

 ... 

<LastGenOutput>db.designer.cs</LastGenOutput>

LastgenOutput 设置为 db1.desginer.cs

Not sure how It did it, but here are some things I worked on to get it back.

Something had it locked, so it generated a new db.designer.cs file (db1.designer.cs).

I had beyond compare open, comparing that file to the previous one (BC isn't supposed to lock and I don't think it was the problem, never had that problem before with it.)

Open the project file in notepad and look for these entries, i revereted to the previous version in source control..

this is what i brought back.

<Compile Include="db.designer.cs">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>db.dbml</DependentUpon>
</Compile>

 ... 

<LastGenOutput>db.designer.cs</LastGenOutput>

the lastgenOutput was set to db1.desginer.cs

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