使用 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."}
基本上,我需要知道如何让构建器在执行脚本时创建此模式。我确信这就是问题所在。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NHibernate 不创建数据库,也不创建模式;只有表和关系。
使用 SchemaExport 之前创建所有架构。
NHibernate does not create databases, nor schemas; only tables and relationships.
Create all the schemas before using SchemaExport.
您可以尝试这种“手动干预”。
这段代码最终让我陷入了 Catch22。
但这是一个可能的解决方法,具体取决于您的数据库是“预先存在的”还是您将其导出为全新的(我认为这会导致 catch22 情况)。
上面的代码,我(在我的原型设计生涯中的某个时刻)放置在下面的 2 条语句之间:
但正如我所说,它可能会导致 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).
The code above, I (at some point in my prototyping life) placed BETWEEN the 2 statements below:
But like I said, it'll probably result in a Catch22.