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# - NHibernate 抛出 'could not resolve property' 异常

NHibernate 返回重复行

c# - 如何使用 nhibernate/linq 进行不区分大小写和串联字段搜索?

c# - .Net 的最佳文档工具

nhibernate - Ninject 和单例

c# - 从类型类列表中删除重复项

c# - 使用 NHibernate 对延迟加载的集合进行分页

NHibernate 一对多关系的聚合查询

c# - MySqlCommand 似乎没有传递参数

C# .net WebSocket 客户端永远不会断开连接