Найкращий спосіб оновити LINQ до класів SQL після зміни схеми бази даних


76

Я використовую LINQ to SQL класи в проекті, де дизайн бази даних все ще трохи змінюється.

Чи існує простий спосіб синхронізації класів зі схемою, чи мені потрібно вручну оновлювати класи, якщо змінюється дизайн таблиці?


Відповіді:


65

Ви можете використовувати SQLMetal.exe для створення вашого файлу dbml та або cs / vb. Використовуйте сценарій попередньої збірки, щоб запустити його та націліть на каталог, де належить ваш проект datacontext.

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>

This is great! I figured I just had to redrag everything into the dbml.
naspinski

14
unfortunately there is no option to filter only the desired tables/sprocs/schemas...
Seiti

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

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


FYI -- there is a free 30 trial available at that site, and a single user license fee of just over $100.
Dan Esparza

Correction, $199 and a trial with limited features. Not sure it's worth it.
Levitikon

8

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.


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.


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!


This is what we're doing...and it's starting to get very annoying! :-)
Richard Ev


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

Використовуючи наш веб-сайт, ви визнаєте, що прочитали та зрозуміли наші Політику щодо файлів cookie та Політику конфіденційності.
Licensed under cc by-sa 3.0 with attribution required.