简体   繁体   English

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

[英]How to update database table schemas with NHibernate schema generation?

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.SchemaExport obj with FluentNHibernate to generate the database schema for a mysql database. 目前我正在使用带有FluentNHibernate的NHibernate.Tool.hbm2ddl.SchemaExport obj来生成mysql数据库的数据库模式。 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. 虽然我不能说这是一个很大的问题,但每当我在数据库上调用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. 所以希望任何答案都会记住这些警告。

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. SchemaUpdate对象通过显示生成并执行一系列SQL UPDATE语句(以及约束语句)来提供数据库模式更新,当它为void Execute(bool script, bool doUpdate)时,调用void Execute(bool script, bool doUpdate)函数。 The SchemaUpdate class is in the NHibernate.Tool.hbm2ddl namespace, which can be found in the Nhibernate.dll file. 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM