Auto Generate DataContext Designer File Using SqlMetal and Visual Studio

I am using SqlMetal for the general DataContext.dbml class for my ASP.net application using LinqToSql. When I originally created the DataContext.dbml file, Visual Studio used it to create the associated DataContext.designer.cs file. This constructor file contains the DataContext class in C #, which is used throughout the application (and derived from XML in the dbml file) and is important for bridging the gap between the SqlMetal output and using the DataContext with LinqToSql.

However, when I make changes to the database and recreate the dbml file, the constructor file is never restored on my website. Instead, the old constructor file is saved (and therefore, none of the changes to the DBML file are available through the DataContext LinqToSql class).

The only process that I have been able to use so far to restore the constructor file,

  • Go to Windows Explorer and delete the dbml and designer.cs files.
  • Go to Visual Studio and click Refresh in Solution Explorer. The dbml and designer.cs files now disappear from the project.
  • Restore dbml file using SqlMetal
  • Go to Visual Studio and click Refresh in Solution Explorer. Now the designer.cs file is recreated.

It appears that Visual Studio will only generate the designer.cs file when it detects a new dbml file that does not yet have the designer.cs file. This process is quite impractical because it involves several manual steps and a mess with source control.

Does anyone know how I can automatically restore the designer.cs file without having to perform the manual delete / update / restore / delete process described above?

+4
source share
2 answers

The designer.cs file is usually supported automatically when you make changes to DBML in Visual Studio. If VS does not work when you recreate DBML, it may not know.

Verify that the .DBML file in Visual Studio has the Custom Tool property set to MSLinqToSQLGenerator. If it is not, then install it. If you try to right-click on DBML after making changes and select "Run Custom Tool" to see if it updates .designer.cs.

The original poster . Run the Custom Tool running inside Visual Studio to restore the designer.cs file.

I also found a better solution - you can generate the class file using SqlMetal: "sqlmetal / code: DataContext.designer.cs / language: csharp DataContext.dbml".

+4
source

Not sure how this happened, but here are some things I worked on to get them back.

Something was blocked, so it generated a new db.designer.cs file (db1.designer.cs).

I had more open comparisons comparing this file with the previous one (BC should not be blocked, and I don’t think it was a problem, never had this problem before).

Open the project file in notepad and look for these entries, I changed to the previous version in the original control.

that's what i returned.

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

lastgenOutput is set to db1.desginer.cs

+3
source

Source: https://habr.com/ru/post/1277165/


All Articles