Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autogeneration of a DataContext designer file when using SqlMetal and Visual Studio

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?

like image 998
Yaakov Ellis Avatar asked Sep 25 '08 14:09

Yaakov Ellis


People also ask

What is a Dbml file?

DBML (Database Markup Language) is an open-source DSL language designed to define and document database schemas and structures. It is designed to be simple, consistent and highly-readable. It also comes with command-line tool and open-source module to help you convert between DBML and SQL.

How do I create a Dbml file?

Choose Add New Item, then under Data, choose Linq-To-SQL Classes . This will create an empty DBML file. You'll then need to create a server connection to a database and drag the tables you want into the DBML designer. Once you save, Visual Studio will regenerate the DBML with entities for your chosen tables.


2 Answers

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
like image 188
DamienG Avatar answered Oct 14 '22 22:10

DamienG


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

like image 4
Doug Avatar answered Oct 14 '22 22:10

Doug