Visual Studio IDE support for keeping up your LINQ-To-SQL entities in sync with the database is weak. I have this low tech solution that can keep your database and your IDE in sync. The task looks bit daunting at first but at the end you are two clicks away from re-generating your LINQ-To-SQL entities. Here are the steps to setup the LINQ sync...
First create CMD file that contains the SQLMetal.exe with instructions for creating your database and add it to your project.
My CMD file is called LINQ_RegenerateDB.cmd. The extension CMD gives you a hint that we will execute this file with the cmd.exe. The content of the this file is this command:
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\SqlMetal.exe" /conn:"Server=.;Database=Northwind;Integrated Security=SSPI;" /dbml:NorthWind.dbml /context:NorthwindDataContext /pluralize /serialization:Unidirectional
/namespace:DataServices /language:C# /views /functions /sprocs
- /conn - is the connection string to your database
- /dbml - creates the .DBML file
- /context - is the name you would give to your class deriving from DataContext
- /pluralize - I like my object names plural
- /serialization - specifying the keyword Unidirectional adds the [DataContract()] attribute on top of every entity
- /namespace - puts the generated classes into namespace of your choice
- /language - directs the custom build tool to use C# syntax
- /views + /functions + /sprocs - instructs SQLMetal to create views, functions and stored procedure wrappers
To execute the .CMD file from IDE go to Tools/External tools and add new external tool CMD like I have in the picture below. Check "Use Output window" so you can monitor when the SQLMetal is finished running in the Output window of the IDE.
To re-generate your DBML file select the .CMD file in your project and on the Tools menu find the CMD menu item. Clicking on the CMD menu item will invoke CMD.EXE and in turn invoke SQLMetal.exe with specifications for re-generating your entire database. I have not found the way to limit what is being generated just yet.
DML file is not in your project just yet so add it from the project folder. Next time you run the cmd.exe IDE will detect the change and ask you to reload if you have the DBML file already open. Click Yes when you see the dialog below.
Every time you regenerate your DBML file all the entities will be stuck in the top left corner. Right click anywhere in the white space and select Layout Diagram to let IDE reposition all the squares around.
Next to last step is to run the Custom Tool to create .CS file with actual entity implementation. To do this right click the DBML file and select Run Custom Tool.
When the custom tool generates the code file it omits one of the constructors. I have created new file with partial class for my data context (Northwind.usercode.cs) and added this constructor my self. This constructor passes the connection string found in your app.config via Settings type.
public partial class NorthwindDataContext
public NorthwindDataContext() :
The final step is to add the NorthwindConnectionString settings. Right click your project and select Properties. On the Setting tab add new row with NorthwindConnectionString like on the picture below.
Next time you only need to run the CMD from your Tools menu and Run Custom Tool. Rest of the settings will be retained.
If someone has found a better way please let me know.