Update LINQ to SQL Classes After Data Table Structure Changed

2011-09-01


How to update LINQ to SQL Class after you changed your data tables? It might be a headache problem if you always use LINQ to SQL.

Because LINQ to SQL is a disconnect architecture, so the normal way is doing update operation in LINQ to SQL Design window by manual:

1: Delete the modified tables from the designer, Click Save to save designer;

2: Refresh the database in Database Explore: Just right click and select refresh;

3: Drag the data table which you modified onto designer, and save designer again;

Above steps always work for us.

Another way is use a tool which provided by Microsoft, this help from StackOverFlow website:

You can use SQLMetal.exe to generate your dbml and or cs/vb file. Use a pre-build script to start it and target the directory where your datacontext project belongs.

C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sqlmetal.exe      
  /server:<SERVER>       
  /database:<database>       
  /code:"path\Solution\DataContextProject\dbContext.cs"       
  /language:csharp       
  /namespace:<your namespace>

However, We have another method:

Do NOT use LINQ to SQL class but use "ADO.NET Entity Data Model", There is a "refresh" menu item directly in right click menu list on designer, so you do not need to delete old tables and add tables again if you use ADO.NET Entity Data Model.

adoentity00