views:

168

answers:

3

I'm creating a data model first using the LINQ-To-SQL graphical designer by using right-click->Add->Class. My idea is that I'll set up everything first using test repositories, design the entire website, then as a final step, create a database using the LINQ-To-SQL classes as a model for the database tables and relationships. My reasoning is that it's easy to edit the classes, but hard to modify DB tables (especially if there's already data in them), so by doing the database part last, it becomes much easier to design the structure.

My question is, is there an automatic way to link the two once I have the DB tables created? I know you can manually fill out the class properties for the LINQ-To-SQL entities but this is pretty cumbersome if you have a lot of tables to deal with. The other option is to delete your manually-created classes and drag the tables from the database into the designer to auto-generate the classes, but I'm not sure if this is the best way of doing it.

+1  A: 

Depending on your database permissions, you may call your datacontext's DeleteDatabase() and CreateDatabase() methods as a ungraceful way of resyncing your classes and tables. This is not much of an option when you have actual data in the database, but does work when you are in your development stages.

Tiko Rollins
A: 

Linq to Sql is intended to be a relatively thin ORM layer over a database. While you can of course just add properties to a data context and use them as a sort of mock, you are correct, it isn't really easy to work with.

Instead of relying solely on Linq to Sql generated classes to give you freedom from the database implementation, you may want to look into the repository design pattern. It allows you to have a smooth separation between your database, domain model, and your middle tier; I have used it on two projects now, and have been able to (for the most part) build everything top-down, leaving the actual database for last. Below is a link to a good tutorial on the pattern (better than I could scribble down here).

http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/10/08/the-repository-pattern.aspx

Dusda
Thanks Dusda, but I'm already using the repository pattern to loosely couple the model to the controller. However, in this case I'm just trying to find out if there's an easy way to link manually-created classes to a database table created after the class itself.
Daniel T.
Possible, yes, but not (in my experience) particularly easy or even worth it. As you said it is difficult to maintain changes between them, especially when making changes to the DBML and then duplicating them on the actual db.It sounds like your architectural preference is firmly in the Domain side of things, so to fit your style I'd recommend an alternate approach entirely. Linq to Sql is a very thin ORM layer; look at NHibernate or perhaps your own custom approach with dependency injection (I use Ninject 2.0 quite heavily for exactly this stuff).
Dusda
A: 

Take a look at my add-in (which you can download from http://www.huagati.com/dbmltools/ , free 45-day trial licenses are also available from the same site).

It can generate SQL-DDL diff scripts with the SQL-DDL statements for updating your database with only the portions that has changed in the L2S model (e.g. add missing columns, missing tables, missing FKs etc), instead of the L2S-out-of-the-box support for recreating the entire db from scratch.

It also supports syncing the other way; updating the model from the database.

KristoferA - Huagati.com