Where should I put my NHibernate SchemaExport method?

297 views Asked by At

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.

3

There are 3 answers

0
DanP On BEST ANSWER

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).

2
Nathan Fisher On

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);
    }
0
Alex On

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.