如何使用 NHibernate 模式生成更新数据库表模式? [英] How to update database table schemas with NHibernate schema generation?

查看:30
本文介绍了如何使用 NHibernate 模式生成更新数据库表模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用带有映射的 NHibernate 配置来更新表模式,而不是删除和重新创建它们.

I'm trying to figure out how to use NHibernate configuration with mapping to update table schemas, rather than dropping and recreating them.

目前我正在使用 NHibernate.Tool.hbm2ddl.SchemaExport obj 和 FluentNHibernate 来为 mysql 数据库生成数据库模式.虽然我不能说这是一个大问题,但每当我在数据库上调用 SchemaExport.Execute 时,它都会删除所有表,然后重新创建它们.

Currently I'm using the NHibernate.Tool.hbm2ddl.SchemaExport obj with FluentNHibernate to generate the database schema for a mysql database. While I can't say it's a huge problem, whenever I call SchemaExport.Execute on the database, it's going to drop all the tables and then recreate them.

如果我可以让它更新现有的表结构,并在可能的情况下保留数据,那会更酷.但我真的不想使用商业产品或代码生成器,因为我一般不喜欢代码生成,而且我不需要它,我会考虑为此付费.所以希望任何答案都能记住这些警告.

What would be way cooler is if I could just have it update the existing table structures retaining data where possible. But I don't really want to use a commerical product, or a code generator, because I don't like code generation in general, and I don't need this enough that I would consider paying for it. So hopefully any answer would keep these caveats in mind.

推荐答案

SchemaUpdate 对象提供数据库模式更新,显然是通过生成和执行一系列 SQL UPDATE 语句(以及约束语句)当它是 void Execute(bool script, bool doUpdate)函数被调用.SchemaUpdate 类位于 NHibernate.Tool.hbm2ddl 命名空间中,该命名空间可以在 Nhibernate.dll 文件中找到.

The SchemaUpdate object provides database schema updating, by apparently generating and executing a series of SQL UPDATE statements (as well as constraint statements) when it's void Execute(bool script, bool doUpdate) function is called. The SchemaUpdate class is in the NHibernate.Tool.hbm2ddl namespace, which can be found in the Nhibernate.dll file.

SchemaUpdate 在 nhibernate 1.0.2 工具集指南的第 15 章中提到,此处(第 15.1.5 节).

SchemaUpdate is mentioned in chapter 15 of the nhibernate 1.0.2 toolset guide, here (section 15.1.5).

NHibernate 常见问题解答"(链接现已过期)提供了一个更完整的关于如何使用 SchemaUpdate 的示例:

"The NHibernate FAQ" had (link now expired) a more complete example of how to use SchemaUpdate:

[Test]
public void Update_an_existing_database_schema()
{
    _cfg = new Configuration();
    _cfg.Configure();
    _cfg.AddAssembly(Assembly.LoadFrom("DataLayer.dll"));
    var update = new SchemaUpdate(_cfg);
    update.Execute(true, false);
}

这篇关于如何使用 NHibernate 模式生成更新数据库表模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆