Linq to SQL class regeneration

I used this great LINQ to SQL tool for the data access layer in an asp.net project. I keep making changes to the base tables and so that the data classes recognize the change that I have to delete and read the table that has changed. Is there a shortcut to restore data level?

+7
linq linq-to-sql
source share
5 answers

I ran into this problem and using sqlmetal is definitely a good way to solve it. One approach is to create a batch file that executes your sqlmetal command, and so you can just run the package anytime you need to update your Linq to SQL classes, but the solution, even smoother, is to use the Visual Studio Tools function -> External Tools create a command in Visual Studio that starts sqlmetal with your parameters. This works great, and you can even delete the created command in the toolbar to restore one click.

+5
source share

You can use sqlmetal, which is a command line class generator for linq to sql classes.

+3
source share

For situations / models where SQLMetal doesn’t quite cut it, for example. due to various naming conventions in the database and your model, or some other settings in your L2S model, I have an add-in for Visual Studio that adds commands to synchronize your L2S designer with the underlying database [schema]. (plus many other features related to L2S and EF)

You can learn more about it, download it and get a 30-day trial license from http://www.huagati.com/dbmltools/

+1
source share

LINQ to SQL version 1 does not support the detection of database schema changes. The only way to modify the generated classes is to restore them either using the constructor or from SQLMetal.

Keep in mind that there are not many differences between SQLMetal and the designer, the designer is a more attractive interface for SQLMetal and hides many of the command line keys.

I use the constructor because I'm too lazy to constantly load the command line.

Also, make sure that you do not write your own code in the generated classes, otherwise you will lose it on regency. All generated classes are partial, which means that you can easily add your own extensions to a separate file.

+1
source share

In the past, where I worked, we created a wrapper class for the DataContext that sqlmetal generated. Then we created a thin data layer that kept the private DataContext and all the classes created by sqlmetal.

If any operations in the software needed information from the database, they had to go through this wrapper layer to get it. In other words, LINQ to SQL cannot appear outside of this data layer.

Thus, whenever we had to regenerate classes through sqlmetal, only parts of the data layer could break. It is much easier to fix one level where the entire data access code is than to change LINQ to SQL spatter in your entire logic or application area.

0
source share

All Articles