流畅的NHibernate -空模式SQL文件

时间:2022-08-14 11:35:01

I am still quite confused about NHibernate schema export and creation. What I want to achieve is to export schema drop-create sql file AND/OR recreate database schema depending on the application configuration.

我仍然对NHibernate模式导出和创建感到非常困惑。我希望实现的是导出模式drop-create sql文件和/或根据应用程序配置重新创建数据库模式。

Obviously I started with

显然,我开始

private void BuildSchema(NHConf.Configuration cfg){
   var schema = new SchemaExport(cfg);
   schema.SetOutputFile(filename);
   schema.Create(true, true);
   schema.Drop(true, true);
}

But recently I have figured out, that what actually causes my schema to recreate is NHConf.Environment.Hbm2ddlAuto set to 'create' and SchemaExport has nothing to it.

但最近我发现,真正导致我的模式重新创建的是nhcon . environment。Hbm2ddlAuto设置为“创建”,SchemaExport没有任何意义。

Also the files with exported SQL schema exists but they are all empty (0KB), which is my main issue, as I manage schema recreation by Hbm2ddlAuto property.

此外,带有导出SQL模式的文件也存在,但它们都是空的(0KB),这是我的主要问题,因为我管理Hbm2ddlAuto属性的模式重构。

Any ideas?

什么好主意吗?

EDIT: The BuildSchema method is called just before cfg.BuildSessionFactory()

编辑:在cfg.BuildSessionFactory()之前调用BuildSchema方法

I use FluentNHibernate with NH 3.1 and Oracle 11g

我使用FluentNHibernate与NH 3.1和Oracle 11g

2 个解决方案

#1


2  

in your method you execute drop-create and then drop and also enabled writing to database.

在您的方法中,您可以执行drop-create,然后drop- up,并启用对数据库的写入。

this is enough to create the files, make sure you set filename correctly

这足以创建文件,请确保正确设置文件名

new SchemaExport(config)
    .SetDelimiter(";")
    .SetOutputFile(filename)
    .Create(false, false);

to create it in database, this works for me

要在数据库中创建它,这对我来说是可行的

new SchemaExport(config).Create(false, true);

#2


1  

If you are using Fluent configuration, check your mapping file for:

如果您正在使用流畅的配置,请检查您的映射文件:

SchemaAction.None();

In my case I commented this line and schema export to file now works!

在我的例子中,我注释了这一行和模式导出到文件现在有效了!

This post moved me in the right direction: http://lostechies.com/rodpaddock/2010/06/29/using-fluent-nhibernate-with-legacy-databases/

这篇文章让我找到了正确的方向:http://lostechies.com/rodpaddock/2010/06/29/using-fluent-nhibernate-with- legaccy -databases/

SchemaAction.None(); The next interesting feature is SchemaAction.None(). When developing our applications I have an integration test that is used to build all our default schema. I DONT want these table to be generated in our schema, they are external. SchemaAction.None() tells NHibernate not to create this entity in the database.

SchemaAction.None();下一个有趣的特性是SchemaAction.None()。在开发应用程序时,我有一个集成测试,用于构建所有默认模式。我不希望这些表在我们的模式中生成,它们是外部的。none()告诉NHibernate不要在数据库中创建这个实体。

#1


2  

in your method you execute drop-create and then drop and also enabled writing to database.

在您的方法中,您可以执行drop-create,然后drop- up,并启用对数据库的写入。

this is enough to create the files, make sure you set filename correctly

这足以创建文件,请确保正确设置文件名

new SchemaExport(config)
    .SetDelimiter(";")
    .SetOutputFile(filename)
    .Create(false, false);

to create it in database, this works for me

要在数据库中创建它,这对我来说是可行的

new SchemaExport(config).Create(false, true);

#2


1  

If you are using Fluent configuration, check your mapping file for:

如果您正在使用流畅的配置,请检查您的映射文件:

SchemaAction.None();

In my case I commented this line and schema export to file now works!

在我的例子中,我注释了这一行和模式导出到文件现在有效了!

This post moved me in the right direction: http://lostechies.com/rodpaddock/2010/06/29/using-fluent-nhibernate-with-legacy-databases/

这篇文章让我找到了正确的方向:http://lostechies.com/rodpaddock/2010/06/29/using-fluent-nhibernate-with- legaccy -databases/

SchemaAction.None(); The next interesting feature is SchemaAction.None(). When developing our applications I have an integration test that is used to build all our default schema. I DONT want these table to be generated in our schema, they are external. SchemaAction.None() tells NHibernate not to create this entity in the database.

SchemaAction.None();下一个有趣的特性是SchemaAction.None()。在开发应用程序时,我有一个集成测试,用于构建所有默认模式。我不希望这些表在我们的模式中生成,它们是外部的。none()告诉NHibernate不要在数据库中创建这个实体。