开发者

Generate Multiple Schemas using Fluent nHibernate

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.


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

Create all the schemas before using SchemaExport.


You can experiment with this "manual intervention".

This code ended up getting me in a Catch22.

But it is a possible work around, depending on if your database is "pre-existing" or you're exporting it entirely new (which causes the catch22 situation I believe).

        using (ISession sess = SomeCodeToGetASessionNotShownHere())
        {
            List<string> schemas = new List<string>();
            schemas.Add("MySchema");


            // SchemaExport won't create the database schemas for us
            foreach (string schema in schemas)
            {

                string sql = string.Format(System.Globalization.CultureInfo.InvariantCulture, "if not exists(select 1 from information_schema.schemata where schema_name='{0}') BEGIN     EXEC ('CREATE SCHEMA {0} AUTHORIZATION dbo;') END", schema);

                sess.CreateSQLQuery(sql).ExecuteUpdate();
            }
        }

The code above, I (at some point in my prototyping life) placed BETWEEN the 2 statements below:

        //drop database 
        new SchemaExport(cfg).Drop(true, true);

         //All the schema-creation Code (from above) goes HERE

        //re-create database
        new SchemaExport(cfg).Create(true, true);

But like I said, it'll probably result in a Catch22.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜