views:

49

answers:

3

Where should I put my NHibernate SchemaExport method and how should I call it when I decide to recreate the database?

Should I leave this in my startup project (an asp.net mvc project)? Should I create a seperate console project just for exporting my schema?

I think these questions all originate from the fact that I don't want schema export to run every time the web app starts.

I'm using fluent nhibernate if that makes a difference.

+1  A: 

As an idea: you could place it in a ProjectInstaller that optionally takes a command line argument. So you wouldn't have to have an extra console app just for that.

Alex
+2  A: 

I would factor this out into a seperate assembly; you could then use this from a variety of places (console app, integration test setup, installer, etc).

DanP
+1  A: 

Personally I use two Tests (using Nunit in this case) to create or update the database. In both cases, I only generate the script, as I want full control as to when the database gets created or updated.

    [Test]
    [Ignore]
    public void Create_Database_Schema_From_MappingFiles()
    {
        Configuration cfg = new Configuration();
        cfg.Configure();
        var schema = new SchemaExport(cfg);

        schema.Create(true, false);
    }

    [Test]
    [Ignore]
    public void Update_an_existing_database_schema()
    {
        Configuration cfg = new Configuration();
        cfg.Configure();
        var update = new SchemaUpdate(cfg);
        update.Execute(true, false);
    }
Nathan Fisher
I'd use explicit instead of ignore. That way you don't have to remove the attribute before running :)
mhenrixon
Thanks Catz. I didn't know about Explict. I was using ignore as an extra check.
Nathan Fisher