bretddog
bretddog

Reputation: 5529

FluentNHibernate, How to validate/check database? (SQL Server Express)

With SQL Server Express and FluentNHibernate:

I map classes, configure SessionFactory and execute SchemaExport; Everything works. But on program-startup; How do you check/verify that the tables exist? Are there some features in Fluent to assist with this?

I imagine it would be proper to have a pop-up box on mismatch, asking if you want to rebuild a fresh database?

Also, are there any other things to validate? (Except obviously that the database exists)

Upvotes: 0

Views: 1477

Answers (1)

Sly
Sly

Reputation: 15227

You can use this code to validate:

    SchemaValidator validator = new SchemaValidator(config);
    try
    {
        validator.Validate();
    }
    catch (HibernateException)
    {
        // not valid, try to update
        try
        {
            SchemaUpdate update = new SchemaUpdate(config);
            update.Execute(false, true);
        }
        catch (HibernateException e)
        {
            MessageBox.Show("invalid schema");
        }
    }

Upvotes: 2

Related Questions