使用 Fluent nHibernate 生成多个模式

发布于 2024-09-26 21:11:43 字数 1933 浏览 1 评论 0原文

我是 nHibernate 和 Fluent nHibernate 的新手,并且在一些简单的设置上遇到了很多麻烦。

    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);
    }

此方法(部分取自他们自己的示例)创建数据库。这一切都很好......

但是我的数据库中有多个模式,例如......

dbo

Sheets.Traits

Sheets 是一个架构。因此,在 SheetsMap 类上,我...

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");
    }
}

但这会在运行时引发错误。知道我应该做什么吗?我真的不明白 99% 这一切是如何运作的。

我收到的错误是......

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."}

基本上,我需要知道如何让构建器在执行脚本时创建此模式。我确信这就是问题所在。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2024-10-03 21:11:43

NHibernate 不创建数据库,也不创建模式;只有表和关系。

使用 SchemaExport 之前创建所有架构。

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

Create all the schemas before using SchemaExport.

镜花水月 2024-10-03 21:11:43

您可以尝试这种“手动干预”。

这段代码最终让我陷入了 Catch22。

但这是一个可能的解决方法,具体取决于您的数据库是“预先存在的”还是您将其导出为全新的(我认为这会导致 catch22 情况)。

        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();
            }
        }

上面的代码,我(在我的原型设计生涯中的某个时刻)放置在下面的 2 条语句之间:

        //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);

但正如我所说,它可能会导致 Catch22。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文