views:

189

answers:

2

I have an ASP.NET web application that includes code for enforcing its own database schema ; this code runs on application start.

I've recently started using LINQ to SQL, and I've added a pre-build event to run SqlMetal on my database so that I get objects representing my db tables.

What would be really cool is if I could enforce the database schema in the pre-build event, and then run SqlMetal. As it is, if the schema changes (e.g. I add a field to a table), I have to (a) build and run the website once so that application start fires and the schema is enforced, and then (b) build it again so that SqlMetal runs.

So: What are my options for running code that lives in my web application, from the command line?

+1  A: 

Here's what we do.

We have a local 1-click build that is required to be run before check in (an integration build also runs in a separate environment every check in...).

The NANT script will:

  1. Rebuild the database from scratch using Tarantino (Database change management)
  2. Clean & Compile
  3. Copy DLLs to a separate directory
  4. Run Unit Tests against the DLLs

We have a separate script for SQL Metal, but your question is going to have me look at inserting the call between steps 1 and 2. This way your database changes and linq generated files are always in sync.

Jim
A: 

You could either write a small program that use CodeDOM to interpret a file in your repository or directly call the compiler executable inside your pre-build event.

Using CodeDOM avoid any problems with having to know where the compiler executable is but if your code can't be contained in one file without any dependency it's unusable and calling the compiler, then executing the result is a better option.

VirtualBlackFox