.net 如何使用 NHibernate 模式生成更新数据库表模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/366176/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:49:25  来源:igfitidea点击:

How to update database table schemas with NHibernate schema generation?

.netnhibernateormfluent-nhibernateschema

提问by Mark Rogers

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

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

Currently I'm using the NHibernate.Tool.hbm2ddl.SchemaExportobj 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.Executeon the database, it's going to drop all the tables and then recreate them.

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

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.

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

回答by Mark Rogers

The SchemaUpdateobject provides database schema updating, by apparently generating and executing a series of SQL UPDATEstatements (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.hbm2ddlnamespace, which can be found in the Nhibernate.dll file.

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

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

SchemaUpdate 在 nhibernate 1.0.2 工具集指南的第 15 章中提到,这里(第 15.1.5 节)。

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

“NHibernate FAQ”有(链接现已过期)一个更完整的关于如何使用 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);
}