views:

44

answers:

1

I'm a newbie to nHibernate, and Fluent nHibernate, and I'm having a great deal of trouble with some simple setup on something.

    private static ISessionFactory CreateSessionFactory()
    {
        return FluentNHibernate.Cfg.Fluently.Configure()
        .Database(
        FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
                  .ConnectionString(@"MultipleActiveResultSets=True;Data Source=.\SQLEXPRESS;Initial Catalog=nHibernate;Integrated Security=True;Pooling=False"))
                  .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
        .ExposeConfiguration(BuildSchema)
        .BuildSessionFactory();
    }

    private static void BuildSchema(NHibernate.Cfg.Configuration config)
    {
        // this NHibernate tool takes a configuration (with mapping info in)
        // and exports a database schema from it
        new SchemaExport(config)
            .Drop(false, true);

        new SchemaExport(config)
            .Create(false, true);
    }

This method (taken partially from their own samples) creates the database. That's all fine and good...

But I have multiple schemas in my database, for instance..

dbo.

Sheets.Traits

Sheets is a schema. So on the SheetsMap class, I have...

public class SheetMap : ClassMap<Sheet>
{
    public SheetMap()
    {
        Id(x => x.Id);
        HasManyToMany(x => x.Traits)
            .ParentKeyColumn("Sheet")
            .ChildKeyColumn("Trait")
            .Cascade.All()
            .Schema("Sheets")
            .Table("Traits");
        Table("Sheets");
    }
}

But this throws an error at runtime. Any idea on what I should do? I'm really not understanding 99% of how this is all supposed to work.

The error I receive is ...

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

{"The specified schema name \"Sheets\" either does not exist or you do not have permission to use it."}

Basically, I need to know how to have the builder create this schema when it is doing the script. That's pretty much where the problem is, I am sure.

+1  A: 

NHibernate does not create databases, nor schemas; only tables and relationships.

Create all the schemas before using SchemaExport.

Diego Mijelshon
I've tried that. It drops them.
Stacey