views:

51

answers:

3

I am using LINQ to SQL with C#. Is there a method through which we can generate entity class files from the table schema? By dragging tables onto the graphical designer classes are generated but they are not the real class files(i mean actual files with the extension cs).

I am aware of that we can code the class files first and then create the schema manually or programmatically, but i wanted to know if the reverse is possible, may be using some third-party tools. I feel it will be very convenient to use LINQ that way.

Thanks in advance.

+1  A: 

All the classes generated when you drag tables to the designer are created as partial classes. There is no reason you can't just create a file for each one and use that to make the necessary modifications.

NickLarsen
I want to create entity classes which are not dependent on LINQ(to have an almost clean domain model and for persistence ignorance).So dragging tables to the designer does not seem to be a solution. Correct me if i am wrong.
amit.codename13
Ahh, from your question I read it as if you wanted something that would generate your classes from the schema for you and support LINQ. Currently you are using LINQ to SQL only you would like your classes each in their own .cs file. Thats what I got from the question anyway. The classes themselves are stored in the MyDatabase.designer.cs file, and you could copy/paste them out of their into your own .cs files and crop out the stuff you don't want. The only problem then would be that the designer would try to create duplicate classes.
NickLarsen
@NickLarsen Thanks. BTW the classes in the designer.cs file contain a lot of blot. I have to crop out many things. Isn't there a more convenient solution to generate clean entity classes. I know that i can programmatically generate the entity classes, but am just curios if anyone has done it already, maybe using some external tools.
amit.codename13
+2  A: 

I'm not as familiar with LINQ to SQL as I am with Entity Framework (v4), but EF certainly would fit your requirements. You can download the POCO templates for EF from Microsoft, right through VS2010 in the Extension Manager (Tool > Extension Manager, click on Online Gallery, and search for POCO). The link is not just the download for the template, but a walkthrough on how to get started.

I also have started a series of blog posts that include some nice T4 templates for an Entity Framework EDMX model that auto generate DTO classes for all of your entity classes, whether you're using the default code generation model, or Microsoft's POCO template. The auto generated DTOs are handy for use in UI or service layers, and save you from having to bring in dependencies on Entity Framework in consuming layers. It's also very easy to get DTOs from your entity objects.

var people = from p in context.People select p;
return people.ToDtos();

Might be worth a look (shameless self promotion).

If you need/want to stick with LINQ to SQL, do a google search for "linq to sql POCO", it seems some people have had a degree of success with this, but most of the search results seem to be from 2008 and earlier, so I'm not sure about currency / relevancy.

Samuel Meacham
+1  A: 

Absolutely you can, if you use the T4 template for L2S - http://l2st4.codeplex.com/

You still use the .DBML file, but you need to set the "build action" to "none" on the file to turn off the compilation of the default code that gets generated. Then you add the .tt file and the .ttinclude file from your codeplex download.

The T4 template has a line of code in it that you can modify to suit your purposes:

FilePerEntity = false, // Put each class into a separate file

Oddly, Entity Framework 4 is using this approach too with the dual methods of generating the code from the model file, but with EF, the T4 template is included with VS2010. With Linq-to-sql, you have to download the T4 template separately. The nice part with using T4 is you can add other customizations as you go. However, initially the code that's generated is identical as what you got from the .DBML designer.

mattmc3