Best way to update LINQ to SQL classes after changing database schema

I am using the LINQ to SQL classes in a project where the database design is still slightly modified.

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

+73
c # linq-to-sql
02 Sep '08 at 16:54
source share
7 answers

You can use SQLMetal.exe to generate your dbml file and / cs / vb. Use a pre-build script to run it and target the directory to which 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> 
+62
Sep 02 '08 at 16:58
source share

I have not tried this 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 for the Linq2SQL / DBML chart designer in Visual Studio 2008, and for the ADO.NET Entity Designer Framework in Visual Studio 2008 SP1. The add-in adds a new menu of options for updating the Linq2SQL diagram designer with database changes, for renaming Linq-to-SQL (DBML) and EF (EDMX) and properties for using .net, and for adding documentation / descriptions to the Generated Linq-to-SQL classes from the database property.

Screenshot of DBML Tools

+17
02 Sep '08 at 16:59
source share

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

Instruction:

  • You pull a copy of the modified table into the constructor (will be deleted later)
  • Now select all new (or changed) fields and ( right-click -> ) copy
  • In the source table, right-click and insert them (delete the changed fields first).
  • Now delete the table you copied from

I know that this is clearly obvious, but somehow unintuitive, and it helped me a lot, since all the necessary attributes and types will be copied, and all links will remain untouched. Hope this helps.

When to use:

Of course, this is - as said - for small changes, but certainly better than manually replacing tables with many links, or when you do not need your entire database structure generated by SQLMetal. For example, when you have a large number of tables (for example, SAP) or when using cross-linked tables from different databases.

+8
03 Feb '14 at 15:51
source share

DamienG has written several t4 templates that can replace some of what VS generates for you. They can be re-run whenever you want using the command line tool.

T4 templates have the added benefit of editing. This allows you to customize what is generated for you, the content of hearts.

+5
Oct 21 '08 at 12:56
source share

I think Jeff recently complained about it. One common way is to drag all the objects back into the constructor ...

Hope someone else buys a better approach!

+4
Sep 02 '08 at 16:56
source share

I wrote a script modification tool in a Dbml script, see http://code.google.com/p/linqtodbmlrunner/ and my blog http://www.adverseconditionals.com

+1
Nov 28 '08 at 18:21
source share

How to change object / table properties on a DataContext design surface in Visual Studio?

For example, if I added a column to the SQL Server table:

  • Open the * .dbml file.
  • Right-click the object and select Add> Property.
  • Fill in the values ​​in the properties window of the new column.
  • Create your solution.

Auto-generated model classes should reflect the new column that has been added.

enter image description here

enter image description here

0
Jun 26 '17 at 22:32
source share



All Articles