开发者

How to perform multiple database alterations at once using NHibernate?

I am looking into using Fluent NHibernate in a desktop application which uses 3 different database files. (Well, 3 different types.)

  • DB1 is the main database file containing tables A, B, C and D.
  • DB2 is a database file containing detailed data (X samples per second, program session will receive a new file as they can grow very large) in table E.
  • DB3 is a database file used to export and import data between users (contains 1 row from A, 1 from B and all of E that belongs to the given row of A and B)

Creating this is not really a problem as the SchemaExport.Create function does this perfectly.

The problem I face is that I don't exactly know what the best way to alter my tables is. More precisely, to do multiple version updates at once. My application is currently version 1, shipping with database format version 1, next week I bump everything to version 2 - I can update the database with SchemaUpdate.Execute - and next month I bump everything to version 3 and I can do the same thing again. But I want my users to be able to update from version 1 to version 3 at once.

If I would do things manually I would create a table called version in each database file which contains a version. I would then check that field against the current version and perform ALTER table queries. For example:

if (db_version < LATEST_VERSION) {
  switch (db_version + 1) {
    case 2: Execute("ALTER TABLE A..."); Execute("UPDATE A..."); break;
    case 开发者_高级运维3: Execute("ALTER TABLE B..."); break;
  }
}

Using this approach (when using the SQLite database provider directly) I would only have to foresee 1 version of my data model, 1 update service which handles everything but I would need to create my database and my data model files manually, and I would have to update at least two .db files and 2 update services (one for each db type). Which can be seen as duplicate work. (I found the database migration of Banshee an example of what I mean by this.)

By using NHibernate I eliminate the process of maintaining a database file and a data model, but the only way I can see on doing updates from 1 to 3 is to keep a legacy version of each data model and do something like:

namespace Version1 {
  public class A {}
  DoUpdate();
}

namespace Version2 {
  public class A {}
  DoUpdate();
}

Am I totally looking at this the wrong way or is my case simply to complex and would I be better off using the manual approach? What would be the best solution to update and migrate from version 1 to 3 using Fluent NHibernate? Or would anyone recommend using the manual approach, and if so why?


I can't answer how to do this with Fluent NHibernate, because I've never figured out a good way. In my opinion, it isn't set up to handle real database migrations. So, for all my Fluent NHibernate projects, I'm using http://code.google.com/p/migratordotnet/ It isn't perfect, but it does the job.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜