为什么设计器在从数据库更新模型时要创建两个 .Designer.cs 文件?

发布于 2024-12-10 10:48:15 字数 233 浏览 0 评论 0原文

就像问题所说:在 Visual Studio 中,在 Model.edmx 中,当我添加一些新的数据库字段后 从数据库更新模型... 时,它会创建一个几乎重复的 Model1.Designer.cs导致与原始 Model.Designer.cs 冲突的文件。

我可以删除新的 Model1.Designer.cs 文件,但新添加的字段不可用。

有没有解决方案(除了删除并重新创建模型)?

Like the question says: In Visual Studio, when in the Model.edmx, when I Update Model from Database... after adding a few new database fields, it's creating an almost duplicate Model1.Designer.cs file that is causing conflicts with the original Model.Designer.cs.

I can delete the new Model1.Designer.cs file, but then the newly added fields aren't available.

Is there a solution to this (other than deleting and recreating the model)?

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

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

发布评论

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

评论(7

亽野灬性zι浪 2024-12-17 10:48:15

听起来您可能已经删除并重新创建了模型(或类似的东西),但将原始设计器文件留在了目录中。然后,当您添加新模型时,它必须使用 Model1 而不是 Model 作为设计器文件名。您是否尝试过排除 Model.Designer.cs 文件并使其与 Model1.Designer.cs 文件一起使用?

好的,查看我们的一个包含模型的项目的项目文件,我可以看到以下可能相关的部分:

<Compile Include="Domain\Model.Designer.vb">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Model.edmx</DependentUpon>
</Compile>

我相信这个部分告诉项目代码文件是项目的一部分,并且应该是文件 model.edmx,并在更改时重新生成。

我们还有此部分:

<EntityDeploy Include="Domain\Model.edmx">
  <Generator>EntityModelCodeGenerator</Generator>
  <LastGenOutput>Model.Designer.vb</LastGenOutput>
  <CustomToolNamespace>Domain</CustomToolNamespace>
</EntityDeploy>

不确定其中哪个控制生成的文件名,但您可以尝试手动编辑项目文件以查看是否有所不同。我想说你需要同时改变两者,而不是只改变一个。

It sounds like you might have deleted and recreated the model (or something similar) but left the original designer file in the directory. Then when you added a new model it had to use Model1 instead of Model as the designer file name. Have you tried excluding the Model.Designer.cs file and leaving it working with the Model1.Designer.cs file instead?

Okay, looking at the project file for a project of ours with a model in, I can see the following potentially relevant sections:

<Compile Include="Domain\Model.Designer.vb">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>Model.edmx</DependentUpon>
</Compile>

I believe this one tells the project that the code file is part of the project, and should be a subnode of the file model.edmx, and be regenerated when it changes.

We also have this section:

<EntityDeploy Include="Domain\Model.edmx">
  <Generator>EntityModelCodeGenerator</Generator>
  <LastGenOutput>Model.Designer.vb</LastGenOutput>
  <CustomToolNamespace>Domain</CustomToolNamespace>
</EntityDeploy>

Not sure which of these controls the generated file name, but you could try hand editing your project file to see if it makes a difference. I'd say you'd need to change both at the same time, rather than just one.

就像说晚安 2024-12-17 10:48:15

原因:

我可以重新创建此问题(并且偶尔会犯错误):在运行项目时尝试保存数据库图表(edmx 文件),导致 Visual Studio 无法写入各种文件,并且生成具有新名称的文件。可能还有其他方法可以通过使文件不可写入来重新创建它。该项目将继续工作,它只会给版本控制带来问题,我想象一些部署模型。

症状:

  • [项目名称]Model1.Designer.cs、[项目名称]Model2.Designer.vb 等文件与 [项目名称]Model.Designer.cs、[项目名称]Model.Designer.vb 文件一起
  • < p>额外的实体文件,形式为[entityname]1.vb,[entityname]1.cs,例如Person1.vb

  • 项目文件引用了冗余文件,例如:

<Compile Include="Models\DataContexts\FooModel.Designer.vb" />

<Compile Include="Models\DataContexts\FooModel1.Designer.vb">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>FooModel.edmx</DependentUpon>
</Compile>
  • 项目文件引用了Model1.Designer、Model2.Designer 等,例如:
 <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
   <Generator>EntityModelCodeGenerator</Generator>
   <LastGenOutput>FooModel1.Designer.vb</LastGenOutput>
</EntityDeploy>
  • 项目文件引用了多个实体或文件名中带有 1.vb 或 1.cs 的实体。
<Compile Include="Models\Entities\Person1.vb" />
  • 项目可以正确构建和运行,但新文件可能不受版本控制。

补救措施

  1. 关闭 Visual Studio
  2. 备份项目文件夹。
  3. 修复文件:
    • 转到项目中包含数据上下文文件的文件夹,例如 Designer.vb、Designer.cs、[项目名称]Model.Designer.vb、[项目名称]Model.edmx
    • 保留重复文件的最新修改版本并删除所有其他版本。
    • 重命名这些文件以删除文件名中的 1、2 等
    • 例如,如果 [项目名称]Model1.Designer.vb、[项目名称]Model2.Designer.vb、[项目名称]Model.Designer.vb 存在并且 [项目名称]Model2.Designer.vb 具有最新修改日期,删除另一个并将 [projectname]Model2.Designer.vb 重命名为 [projectname]Model.Designer.vb。
    • 对附加有重复项或 1.vb、1.cs 的任何其他文件执行相同的操作。
  4. 修复项目文件。使用您喜欢的 xml 或文本编辑器打开项目文件并修复以下引用。 (搜索“1.”、“Model1”、“2.”、“Model2.”也会找到这些。)
Change:

    <Compile Include="Models\DataContexts\FooModel.Designer.vb" />
    <Compile Include="Models\DataContexts\FooModel1.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>FooModel.edmx</DependentUpon>
    </Compile>

To:

    <Compile Include="Models\DataContexts\FooModel.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>FooModel.edmx</DependentUpon>
    </Compile>



Change:
    <Compile Include="Models\DataContexts\Person1.vb">
      <DependentUpon>Foo.tt</DependentUpon>
    </Compile>

To:

    <Compile Include="Models\DataContexts\Person1.vb">
      <DependentUpon>Foo.tt</DependentUpon>
    </Compile>



Change:
    <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
      <Generator>EntityModelCodeGenerator</Generator>
      <LastGenOutput>FooModel1.Designer.vb</LastGenOutput>
    </EntityDeploy>


To:

    <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
      <Generator>EntityModelCodeGenerator</Generator>
      <LastGenOutput>FooModel.Designer.vb</LastGenOutput>
    </EntityDeploy>
  1. 打开 Visual Studio,一切都应该很好。

Cause:

I can recreate this (and do by mistake now and then): it occurs trying to save the database diagram (edmx file) while running a project such that Visual Studio cannot write to the various files and generates ones with new names. There may be other ways to recreate it by making the files unavailable for writing. The project will keep working, it just creates a problem for version control and I imagine some deployment models.

Symptoms:

  • [projectname]Model1.Designer.cs, [projectname]Model2.Designer.vb, etc. files alongside [projectname]Model.Designer.cs, [projectname]Model.Designer.vb files
  • extra enitity files in form of [entityname]1.vb, [entityname]1.cs such as Person1.vb

  • project file has references to redundant files such as:

<Compile Include="Models\DataContexts\FooModel.Designer.vb" />

<Compile Include="Models\DataContexts\FooModel1.Designer.vb">
  <AutoGen>True</AutoGen>
  <DesignTime>True</DesignTime>
  <DependentUpon>FooModel.edmx</DependentUpon>
</Compile>
  • project file has reference with Model1.Designer, Model2.Designer etc such as:
 <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
   <Generator>EntityModelCodeGenerator</Generator>
   <LastGenOutput>FooModel1.Designer.vb</LastGenOutput>
</EntityDeploy>
  • Project file has references to multiple entities or entities with 1.vb or 1.cs in the filename.
<Compile Include="Models\Entities\Person1.vb" />
  • Project may build and run correctly, but new files may not be in version control.

Remedy

  1. Close Visual Studio
  2. Make a backup of the project folder.
  3. Fix the files:
    • Go to the folder in the project with the data context files such as Designer.vb, Designer.cs, [projectname]Model.Designer.vb, [projectname]Model.edmx
    • Keep the most recently modified version of the duplicate files and delete all others.
    • Rename those files to remove the 1s, 2s, etc from the filenames
    • For example, if [projectname]Model1.Designer.vb, [projectname]Model2.Designer.vb, [projectname]Model.Designer.vb exist and [projectname]Model2.Designer.vb has the most recent modified date, delete the other to and rename [projectname]Model2.Designer.vb to [projectname]Model.Designer.vb.
    • Do the same with any other files that have duplicates or 1.vb, 1.cs appended to them.
  4. Fix the project file. Open up the project file with your favorite xml or text editor and fix the following references. (A search for "1.", "Model1", "2.", "Model2." will find these also.)
Change:

    <Compile Include="Models\DataContexts\FooModel.Designer.vb" />
    <Compile Include="Models\DataContexts\FooModel1.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>FooModel.edmx</DependentUpon>
    </Compile>

To:

    <Compile Include="Models\DataContexts\FooModel.Designer.vb">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>FooModel.edmx</DependentUpon>
    </Compile>



Change:
    <Compile Include="Models\DataContexts\Person1.vb">
      <DependentUpon>Foo.tt</DependentUpon>
    </Compile>

To:

    <Compile Include="Models\DataContexts\Person1.vb">
      <DependentUpon>Foo.tt</DependentUpon>
    </Compile>



Change:
    <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
      <Generator>EntityModelCodeGenerator</Generator>
      <LastGenOutput>FooModel1.Designer.vb</LastGenOutput>
    </EntityDeploy>


To:

    <EntityDeploy Include="Models\DataContexts\FooModel.edmx">
      <Generator>EntityModelCodeGenerator</Generator>
      <LastGenOutput>FooModel.Designer.vb</LastGenOutput>
    </EntityDeploy>
  1. Open up visual studio and all should be well.
棒棒糖 2024-12-17 10:48:15

今天发生在我身上,我通过以下方法解决了这个问题:

  1. 如果打开了 edmx 编辑器窗格,请将其关闭。
  2. 在解决方案资源管理器中,删除有问题的设计器文件。
  3. 全部保存
  4. 将 edmx 文件重命名为其他名称(例如 2.edmx)
  5. 全部保存
  6. 打开 edmx 文件并保存以重新生成设计器文件。
  7. 在解决方案资源管理器中,将 edmx 文件重命名回其原始名称。

This happened to me today, and I solved it by:

  1. Close the edmx editor pane if you have it open.
  2. In the solution explorer, delete the offending designer file.
  3. Save all
  4. Rename the edmx file to a different name (such as 2.edmx)
  5. Save all
  6. Open the edmx file and save it to regenerate the designer file.
  7. In the solution explorer, rename the edmx file back to its original name.
余生共白头 2024-12-17 10:48:15

我遇到了同样的问题,模型正在生成第二个 Designer.cs 文件。这是在我安装 vs2010 时解决了与设计器相关的一大堆问题之后的结果(不知何故,vs2010 丢失了对与数据实体模型设计器相关的一堆 DLL 的引用,需要重新编辑和重新安装)。

根据凯文的建议,我手动编辑了项目文件以指向我喜欢的文件,并且它起作用了。设计者停止重新创建第二个文件。

I had the same problem, the model was generating a second designer.cs file. This was after working through a whole bunch of issues with my installation of vs2010 related to the designer (somehow vs2010 lost the references to a bunch of DLL's related to the Data Entity Model designer requiring rededits and reinstalls).

Taking Kevin's advice, I edited the project file manually to point to my preferred file and it worked. The designer stopped recreating the second file.

夏了南城 2024-12-17 10:48:15

如果您使用任何版本控制实用程序(例如 SVN),最简单的修复方法是将工作副本与以前的修订版本进行比较,并恢复 .csproj 中设计者名称更改为带有“1”后缀的一行。

If you are using any version control utils like SVN, the easiest way to fix it is to compare working copy with previous revisions and revert one line in .csproj where designer name changed to that with '1' suffix.

策马西风 2024-12-17 10:48:15

遇到了同样的问题,并且旧的答案都不适合我。额外的设计师不断重新出现。

我的原因(在 .csproj 中):

 <Compile Include="DAL\MyDataSet.cs">
   <DependentUpon>MyDataSet.xsd</DependentUpon>
   <SubType>Component</SubType>
 </Compile>

解决方案:

 <Compile Include="DAL\MyDataSet.cs">
   <DependentUpon>MyDataSet.xsd</DependentUpon>
 </Compile>

额外的 SubType 导致 MSDataSetGenerator 每次运行两次。删除子类型后,您仍然需要删除任何其他错误生成的 MyDataSet1.Designer.cs 条目。请参阅 Kevin 的帖子了解几个示例。

Had the same issue, and none of the older answers worked for me. The extra designer kept re-appearing.

My Cause (in the .csproj):

 <Compile Include="DAL\MyDataSet.cs">
   <DependentUpon>MyDataSet.xsd</DependentUpon>
   <SubType>Component</SubType>
 </Compile>

Solution:

 <Compile Include="DAL\MyDataSet.cs">
   <DependentUpon>MyDataSet.xsd</DependentUpon>
 </Compile>

The extra SubType caused MSDataSetGenerator to run twice each time. After removing the subtype, you will still need to delete any other erroneously generated MyDataSet1.Designer.cs entries. See Kevin's post for a couple of examples.

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