Skip to main content
 首页 » 编程设计

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

2024年02月24日20zhujiabin

我是 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 之前创建所有模式。