Fluent NHibernate - 仅在不存在时创建数据库模式
我有一个应用程序,我使用 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);
}
请注意“if (DbExists(config))
”。这就是我想做的。仅当架构实际上尚不存在时,我才想创建该架构。下一步 - 我想更新 如果它不是最新的,则要创建它。
我该如何实现这一目标?我期待一个 config.DatabaseExists()
,但我看不到这样的东西。我看到了一些黑客解决方案的可能性,但是处理这个问题的典型推荐方法是什么?
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:
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以只使用
SchemaUpdate
相反,它会更新架构(如果存在)并创建它(如果不存在):一个警告:
SchemaUpdate
不会执行破坏性更新(删除表、列等)。它只会添加它们。You can just use
SchemaUpdate
instead, it will update the schema if it exists and create it if it does not:One caveat:
SchemaUpdate
does not do destructive updates (dropping tables, columns, etc.). It will only add them.更新(感谢 dotjoe)
Hbm2ddl 只能进行模式比较,并且只能更新 SchemaUpdate 类更改的内容。然而,这个类非常初级,因为它只查看当前实体以及模式的不同之处。如果进行了重大更改(即删除了实体或删除了链接表),它将无法弄清楚这一点。
在早期的项目中,我们使用了 hbm2ddl,但此后我们转而使用 Fluent Migrator。我想说你最好的选择是使用迁移工具,例如 Fluent Migrator 或 Migrator.NET。
http://github.com/schambers/fluencemigrator/
http://code.google.com/p/migratordotnet/
UPDATED (thanks 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.
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.
http://github.com/schambers/fluentmigrator/
http://code.google.com/p/migratordotnet/