DevExpress XPO vs NHibernate vs Entity Framework: database update issue

What is the best practice for updating a database using ORM ( DevExpress XPO , NHibernate, or MS Entity Framework )?

I am starting a new project and must choose ORM. The development process often requires the release of intermediate test collections and it is likely that each assembly will have changes in the database structure. Each new version should update the database carefully to preserve current data.

For older solutions, I would provide a set of SQL scripts for updating the database from v1 to v2, from v2 to v3, etc. and execute them sequentially.

But how will this work for ORM? Should I write SQL scripts to update the database?

I understand that simply adding new fields will not cause a problem (for example, see the UpdateSchema () method for XPO), but what if I need to split the table and redistribute the current records into 2 new tables?

+6
source share
4 answers

I cannot comment on other ORMs, but I have been using DevExpress XPO for a corporate treasury application since 2007. The layout changes slightly with each version, but over the years there have also been some changes to the layout. The somewhat expanded version of the XPO update mechanism is by default convenient for all changes.

There is good basic information here about updating XPO applications.

  • DevExpress provides a DBUpdater tool to help you upgrade your production environments. You can expand this tool to meet additional requirements. In my application, we added some options for logging, rollback previews, etc.

  • Each module has the virtual methods UpdateDatabaseBeforeSchemaUpdate() and UpdateDatabaseAfterSchemaUpdate() . You can significantly control the update process in them.

As you noticed, some updates will be handled automatically by XPO (for example, by adding a new column), but some things need additional control, such as initializing a new column with a default value for existing records.

For example, let's say MyNewField was added to the MyEntity XPO class in version 2.0 of your application. Suppose that existing records should be set to 3. XPO will process the creation of a new column, but existing records will be NULL. (If you specify a default value in the XPO class, it will only apply to new entries). To correct the value for existing records, you would add something like the following to the overridden UpdateDatabaseAfterSchemaUpdate() module:

 public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); if (CurrentDBVersion < new Version(2, 0, 0, 0)) ObjectSpace.GetSession().ExecuteNonQuery( "UPDATE [MyEntity] SET [MyNewField] = 3 WHERE [MyNewField] IS NULL"); } 

(You can also use ObjectSpace.GetObjects<MyEntity>() and foreach if you want to avoid direct SQL.)

In your more extreme example of splitting a table into two parts, you can use the same method, but instead you override UpdateDatabaseBeforeUpdateSchema() , run SQL to split the table, let XPO do any other schema updates, and enter default values ​​if necessary UpdateDatabaseAfterUpdateSchema() .

You will find that you are experiencing restriction problems, such as foreign key violations, so you may need to write some common routines such as DropAllForeignKeyConstraints() as part of UpdateDatabaseBeforeUpdateSchema() . Sometimes you find that XPO already provides something, sometimes not. Missing restrictions and indexes will be restored in updating the schema. (In my experience, switching the primary key of the master data table has proven to be the most difficult upgrade procedure to qualify.)

By default, all calls occur in an SQL transaction, so if something fails, everything should be rolled back.

Developers should know when changing a domain model can cause a problem with the underlying schema.

For testing purposes, we save several old client databases and run tests before and after testing as part of the build process to ensure that existing clients can correctly update any version from which they are updated. During production, whenever we encounter an update problem, these problems are added to this test library to prevent similar problems in the future.

We deal with large international companies and banks. The result is quite satisfied. In situations where the corporate database administrator needs to subscribe to the changes, they don't seem to need a command-line tool for updating, not a script.

+6
source

Most migration solutions can handle simple tasks, such as adding a new column, a relationship, or deleting, but don't work when you rename a column (is it adding? Or deleting after adding, which is equal to renaming? What to do with the data in this case?)

All three solutions support basic migrations, XPO even allows you to run your own scripts as part of the process (for inserting static / test / contact data, etc.).

There is also a MigratorDotNet project that you can use and not rely on any ORM feature related to migrations.

Personally, I would use automatic migration only in the dev / test environment and would have a full set of upgrade scripts when working in the client database, to say that the update is from v1 to v2.

0
source

How does it work for ORM? Should I write SQL scripts to update the database?

A clear answer to this question should be on the Threader file Programmer - What are the criteria for evaluating ORM for .NET? , there I received a simple answer to your question that you asked and compared my experience with ORM when developing a project using Entity templates and ORM Code smith templates.

How does ORM manage changes to the data model? What if I need to split the table and redistribute the current records into 2 new tables?

Some may automatically update the database to a certain extent, others do nothing, and you will have to do the dirty work yourself; Others provide a structure for processing changes that allows you to manage the Updates database. This means that every couple of days someone needs to spend an hour updating the model to add a table or change data types that are changing

Ref:
https://softwareengineering.stackexchange.com/questions/6543/what-are-the-benefits-of-using-database-abstraction-by-orm
https://softwareengineering.stackexchange.com/questions/41739/best-arguments-for-against-introducing-orm-technology-into-a-companies-dev-proce/41833#41833

0
source

If you ask what is best for updating db using ORM, my answer is: do not use it if your application is larger than the application for fans.

There are many scenarios in which many ORMs cannot provide support for your specific database needs, for example. in creating stored procedures, creating indexes and views, or even indexed views / materialized tables without writing sql scripts. The problems associated with adding a new non-zero column to an existing table are much more difficult to solve in ORM-Migration-Code than by writing SQL scripts.

Current tools, such as Visual Studio data processing tools, deal with such problems better.

0
source

Source: https://habr.com/ru/post/916221/


All Articles