Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the designer creating two .Designer.cs files when updating model from database?

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)?

like image 979
Doozer Blake Avatar asked Oct 18 '11 13:10

Doozer Blake


People also ask

What is a designer cs file?

The designer file (. Designer. cs) is a code file automatically generated by your designer to hold the form's layout information that was created using the Visual Studio IDE. Once you add a new form in your application, VS will automatically generate the designer file for this form.

What is EDMX designer?

edmx is basically an XML file which is generated when we added Entity Framework model. It is Entity Data Model Xml which contains designer (Model) and code file(. cs). As you can see in following image the .edmx file in solution explorer.


3 Answers

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.
like image 171
user2048773 Avatar answered Oct 12 '22 23:10

user2048773


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.

like image 37
Kevin O'Donovan Avatar answered Oct 13 '22 01:10

Kevin O'Donovan


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.
like image 27
Rick Velde Avatar answered Oct 13 '22 01:10

Rick Velde