LINQ-to-SQL: Be careful of CreateDatabase()
I have recently started work on what will become our first production application using LINQ-to-SQL and had hoped to use the CreateDatabase() function that is found on the generated DataContext to simplify the process of setting up the database on the client machine.
Ideally I wanted a nice simple piece of code like this
AdventureWorksDataContext db = new AdventureWorksDataContext(); if (!db.DatabaseExists()) { db.CreateDatabase(); }
At first glance this works great, you get a shiny new database created with all the right tables and relationships. The devil however is in the detail.
Because LINQ-to-SQL only models the database relationships, some important information is not stored in the DataContext and therefore will not propagate with a CreateDatabase() call. This includes (but is not limited to) Default Field Values and Triggers.
Now it is possible to code around this. If you like, both can be handled in code by hooking into the OnValidate() partial method of your generated data classes. See here for a previous post about partial methods, though not specifically that method.
However the next problem is not so easy to code around. Although a relationship such as FK_OrderItems_Orders will be created in your new database, it will not necessarily have the same name as the relationship in your master database. In fact the two don't even follow the same naming standard (LINQ-to-SQL leaves out the FK_) so they are almost certain not to have the same name.
What this means is that you can not write a SQL script against the master database to be rolled out onto the client databases with a future upgrade.
Sadly this renders the CreateDatabase() call basically useless in anything other than the simplest applications.
I would love to see LINQ-to-SQL in the future ship with a set of tools that wrapped up proper database creation and upgrades for you and could reduce it to a piece of code as simple as that above. Now that would be pretty special.

2 Comments:
Chris,
I have yet to check out the CreateDatabase() method but I was really looking forward to it. I will now approach w/ caution. Thanks for the tip.
You mention that you are about to start your first project w/ LINQ. If you don't mind sharing, what kind of approach are you taking to leverage LINQ? I am having a very hard time trying to keep a clean separation of my UI layer and other potential layers. Is there one particular pattern that you found to work best when trying to use LINQ?
Thanks.
The project is in ASP.NET and using the new MVC engine they have recently released. Taking the time to implement the MVC pattern correctly is providing for good separation of logic.
Things are still in their simple stages though, as I start to deal with a few more complex pages I am sure to have to deal with some quirks.
The only place LINQ-to-SQL might let you down is building a data layer, it is hard to provide a generic base class for LINQ-to-SQL due to the strong typing of everything. Rick Strahl has posted about these issues.
Post a Comment
<< Home