c# - 使用 Fluent nHibernate 生成多个模式

标签 c# nhibernate fluent-nhibernate

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

基本上,我需要知道如何让构建器在执行脚本时创建此架构。我敢肯定,这几乎就是问题所在。

最佳答案

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

在使用 SchemaExport 之前创建所有模式。

关于c# - 使用 Fluent nHibernate 生成多个模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3910819/

相关文章:

c# - StackExchange.Redis在redis集群模式下有什么用

c# - 将验证实现为方法还是属性?

entity-framework - DevExpress XPO 与 NHibernate 与 Entity Framework : database upgrading issue

nhibernate - Fluent NHibernate - 加入子类ForeignKey Name

c# - 无法使用 NHibernate 加入分布式事务

c# - 在 C# ASP.NET MVC 5 中将图像上传到 S3

c# - 如何通过策略覆盖以编程方式将代码 checkin TFS 中?

c# - NHibernate QueryOver 将一个属性合并到另一个属性

c# - NHibernate 和 AutoMapper 不能正常运行 : "a different object with the same identifier value was already"

c# - ASP.NET 4 MVC 十进制溢出与 Fluent NHibernate 和 SQL Server 2008 RC2