.net Fluent NHibernate - 仅当不存在时才创建数据库模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5884359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fluent NHibernate - Create database schema only if not existing
提问by stiank81
I have an application where I use Fluent Nhibernate to create my database. This far I've been recreating the database schema each time. The code that does this is this:
我有一个应用程序,我使用 Fluent Nhibernate 创建我的数据库。到目前为止,我每次都在重新创建数据库模式。这样做的代码是这样的:
public NhibernateSessionFactory(IPersistenceConfigurer config)
{
_sessionFactory = Fluently.Configure().
Database(config).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()).
ExposeConfiguration(BuildSchema).
BuildSessionFactory();
}
private static void BuildSchema(Configuration config)
{
// if (DbExists(config))
// return;
new SchemaExport(config).Create(false, true);
}
Note the "if (DbExists(config))". This is what I'd like to do. I'd like to create the schema only if it actually doesn't already exist. And in the next step - I'd like to update
it to be created if it isn't up to date.
注意“ if (DbExists(config))”。这就是我想做的。仅当它实际上不存在时,我才想创建模式。在下一步 - 如果它不是最新的,我想更新它以创建它。
How do I achieve this? I am expecting a config.DatabaseExists(), but I can't see anything like this. I see some possibilities of a hacky solution, but what is the typical recommended way of handling this?
我如何实现这一目标?我期待一个config.DatabaseExists(),但我看不到这样的东西。我看到了一些 hacky 解决方案的可能性,但是处理这个问题的典型推荐方法是什么?
回答by Gabe Moothart
You can just use SchemaUpdateinstead, it will update the schema if it exists and create it if it does not:
您可以SchemaUpdate改为使用,如果存在,它将更新架构,如果不存在,则创建它:
public NhibernateSessionFactory(IPersistenceConfigurer config)
{
_sessionFactory = Fluently.Configure().
Database(config).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>()).
ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true)).
BuildSessionFactory();
}
One caveat: SchemaUpdatedoes not do destructive updates (dropping tables, columns, etc.). It will only add them.
一个警告:SchemaUpdate不进行破坏性更新(删除表、列等)。它只会添加它们。
回答by Vadim
UPDATED(thanks dotjoe)
更新(感谢 dotjoe)
Hbm2ddl is only capable of doing a schema diff and only updating what has changed with the SchemaUpdate class. However this class is pretty rudimentary in that it only takes a look at the current entities and how the schema differs. If significant changes have been made (ie entities removed or link tables removed) it won't be able to figure that out.
Hbm2ddl 只能进行模式差异,并且只能更新使用 SchemaUpdate 类更改的内容。然而,这个类非常初级,因为它只查看当前实体以及模式的不同之处。如果进行了重大更改(即删除了实体或删除了链接表),它将无法弄清楚。
On an earlier project we used hbm2ddl, however we've since moved onto use Fluent Migrator. I would say your best bet is to use a migration tool, like Fluent Migrator or Migrator.NET.
在较早的项目中,我们使用了 hbm2ddl,但此后我们转而使用 Fluent Migrator。我认为最好的办法是使用迁移工具,例如 Fluent Migrator 或 Migrator.NET。
http://github.com/schambers/fluentmigrator/
http://github.com/schambers/fluentmigrator/

