如何更新数据库表的模式与NHibernate的模式生成?模式、数据库、NHibernate

2023-09-02 20:45:10 作者:天空微晴

我试图找出如何使用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.

什么是凉的方式是,如果我能有它更新现有的表结构保存数据在可能的情况。但我真的不希望使用商用产品,或code发电机,因为我不喜欢一般code的产生,我不需要这还不够,我​​会考虑为它付出。所以希望所有的答案将保留这些告诫记在心里。

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 语句(以及约束语句)时,它的无效执行(布尔脚本,布尔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);
}