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

查看:44
本文介绍了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(aka 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天全站免登陆