NHibernate、ORM:重构是如何处理的?现有数据? [英] NHibernate, ORM : how is refactoring handled? existing data?

查看:12
本文介绍了NHibernate、ORM:重构是如何处理的?现有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 ORM(特别是 NHibernate)时,如何处理数据重构?我的意思是,如果数据对象发生变化以至于底层关系数据库模式发生变化,ORM 是否在数据库中的模式升级/降级方面提供任何帮助?如何将现有数据迁移到新架构?

When using an ORM (specifically NHibernate), how is refactoring of data handled? I mean, if data objects change such that the underlying relational DB schema changes, does the ORM offer any help in schema upgrades / downgrades in the DB? How about migration of existing data to the new schema?

我正在决定使用 ORM 并且接触非常有限.如果问题很幼稚,请多多包涵.

I am in the process of making a decision on using an ORM and have very limited exposure. Please bear with me if the questions are naive.

推荐答案

在 NHibernate 中,您可以使用 SchemaUpdate 类对您的架构进行附加更改.(附加更改将包括新表、新列等,但不包括删除.) SchemaUpdate 旨在用于开发目的,不打算在生产中运行.我强烈建议您使用 SQL 迁移工具,例如 Tarantino、dbdeploy.net、RikMigrations 或类似工具.

In NHibernate, you can use the SchemaUpdate class to make additive changes to your schema. (Additive changes would include new tables, new columns, etc., but not deletes.) SchemaUpdate is intended for development purposes and is not intended to be run in production. I would strongly recommend checking out a SQL migration tool such as Tarantino, dbdeploy.net, RikMigrations, or similar.

迁移工具有两种形式——基于 SQL 脚本的(Tarantino 和 dbdeploy.net)和基于代码的(RikMigrations 和 Rails 风格的迁移).借助基于代码的迁移工具,您可以使用用 C#、VB、Ruby 编写的代码编写迁移……基于 SQL 脚本的工具采用一组有序的 SQL 脚本.在任何一种情况下,迁移工具都会针对之前未运行过的数据库运行任何迁移.(通常,迁移表会列出已运行的脚本,并允许该工具确定哪些仍需要运行.)SQL 脚本通过以下方式生成:

Migration tools come in two flavours - SQL script-based (Tarantino and dbdeploy.net) and code-based (RikMigrations and Rails-style migrations). With a code-based migration tool, you write your migrations using code written in C#, VB, Ruby, ... SQL script-based tools take an ordered set of SQL scripts. In either case the migration tool runs any migrations against your database that haven't been run before. (Typically a migrations table lists the scripts that have been run and allows the tool to figure out which still need to be run.) The SQL scripts are generated via:

// SchemaUpdate.Execute(bool script, bool doUpdate)
new SchemaUpdate(cfg).Execute(true, false); 

然后根据口味进行编辑.或者,您可以使用 NHibernate 的 SchemaExport 并使用架构差异工具(例如 Microsoft Visual Studio for Database Professionals Ultimate Now With Extra Mayo Edition(又名 DataDude)或 RedGate SQL Compare)生成新架构.您需要手动编写转换脚本,因为 SQL 迁移工具通常无法知道填充 T/F 的 Foo char(1) 列应转换为该 Bar 位列.

and then edited to taste. Or you could generate a new schema using NHibernate's SchemaExport and using a schema diff tool such as Microsoft Visual Studio for Database Professionals Ultimate Now With Extra Mayo Edition (aka DataDude) or RedGate SQL Compare. You would need to write transformation scripts by hand as there is no way in general for the SQL migration tool to know that that Foo char(1) column filled with T/F should be transformed into that Bar bit column.

我个人更喜欢基于 SQL 脚本的迁移工具,因为我可以使用工具生成架构差异,然后根据喜好进行编辑,而不必使用 C# 或类似语言手动完成整个迁移.

Personally I prefer the SQL script-based migration tools as I can generate the schema diffs using a tool and then edit to taste rather than having to hand-roll the entire migration using C# or similar language.

这篇关于NHibernate、ORM:重构是如何处理的?现有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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