76

I'm using LINQ to SQL classes in a project where the database design is still in a bit of flux.

Is there an easy way of synchronising the classes with the schema, or do I need to manually update the classes if a table design changes?

Compile This
  • 11,892
  • 2
  • 25
  • 22
  • possible duplicate of [How to update Linq to SQL dbml file?](http://stackoverflow.com/questions/1110171/how-to-update-linq-to-sql-dbml-file) – Brian Webster Oct 07 '12 at 04:19

7 Answers7

65

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>
vzczc
  • 9,270
  • 5
  • 52
  • 61
  • This is great! I figured I just had to redrag everything into the dbml. – naspinski Sep 19 '08 at 07:17
  • 14
    unfortunately there is no option to filter only the desired tables/sprocs/schemas... – Seiti Dec 22 '08 at 16:42
  • I created a utility to help with the filtering. It lets you choose the tables you want to exclude, and also to select the tables where you want the updatecheck.never for non pk columns. Let me know if anyone wants the source code. – vzczc Apr 25 '13 at 08:13
17

I haven't tried it myself, but Huagati DBML/EDMX Tools is recommended by other people.

Huagati DBML/EDMX Tools is an add-in for Visual Studio that adds functionality to the Linq2SQL/DBML diagram designer in Visual Studio 2008, and to the ADO.NET Entity Framework designer in Visual Studio 2008 SP1. The add-in adds new menu options for updating Linq2SQL designer diagrams with database changes, for renaming Linq-to-SQL (DBML) and EF (EDMX) classes and properties to use .net naming conventions, and for adding documentation/descriptions to Linq-to-SQL generated classes from the database properties.

Screenshot of DBML Tools

Neysor
  • 3,893
  • 11
  • 34
  • 66
Espo
  • 41,399
  • 21
  • 132
  • 159
9

Here is an easy fix without any additional software, that just works for simple changes (like added fields, few tables, etc).

Instructions:

  • You pull a copy of the changed table into the designer (will be removed later)
  • Now select all the new (or changed) fields and (right-click ->) copy
  • In your original table right click and insert them (delete changed fields first)
  • Now delete the table you copied them from

I know it is kinda obvious, but somehow non-intuitive, and it helped me a lot, since all the right attributes and types will be copied, and all links stay intact. Hope it helps.

When to use:

Of course it is - as said - for small changes, but surely better than manually replacing tables with many links, or when you don't want your whole database structure generated by SQLMetal. For example when you have a big amount of tables (e.g. SAP), or when using cross-linked tables from different databases.

Levite
  • 17,263
  • 8
  • 50
  • 50
  • Is there some secret to the "insert" step. Each time I do it, it creates a new tableadapter with just the one column in it – Greg Woods Jul 23 '22 at 07:51
  • @GregWoods No, actually not. Just selecting the correct entity class and afterwards pressing Ctrl+V or `right-click` -> `insert` on it. – Levite Jul 27 '22 at 08:53
5

DamienG has written some t4 templates which can replace some of what VS generates for you. These can be rerun whenever you like via a command line tool.

T4 templates have the added benefit of being editable. This allows you to tweak what is generated to you hearts content.

Rory Becker
  • 15,551
  • 16
  • 69
  • 94
4

I think Jeff complained about this recently. One common technique is to drag all the objects into the designer again...

I hope someone else chimes in with a better approach!

Michael Haren
  • 105,752
  • 40
  • 168
  • 205
1

I wrote a tool to do script changes to Dbml scripts see http://code.google.com/p/linqtodbmlrunner/ and my blog http://www.adverseconditionals.com

mcintyre321
  • 12,996
  • 8
  • 66
  • 103
0

How about modifying the Properties of the entity/table within the DataContext design surface within Visual Studio?

For instance if I added a column to an SQL Server table:

  1. Open the *.dbml file.
  2. Right click the entity and select Add > Property.
  3. Fill out the values in the Properties window for the new column.
  4. Build your solution.

The auto generated model classes should reflect the new column that was added.

enter image description here

enter image description here

Dean
  • 79
  • 1
  • 2