NHibernate, ORM : how is refactoring handled? existing data? - nhibernate

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?
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.

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.
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);
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.
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.

Related

EF6 Schema compare between database and sql-file?

I use EF6 model-first approach to design MS SQL database by using "Generate database from model..." in Visual Studio.
This generates a sql-file with all drop and create statements.
At some productive stage where the database is filled with records, I do not want all tables to be dropped and recreated, their scheme should be updated in place without the need to backup and restore all records. That's where I found EF Migrations would be useful.
As I read, EF migrations are only suitable for code-first approach - so not what I need.
Next stop is the Schema Comparison Tool in Visual Studio, from which I think, it could fit.
But unfortunately I do not have 2 databases to compare, what I want to compare is the existing database to the new EF model (or the SQL-file generated out from that) to get a SQL-file for deploying only changes of tables.
Is this possible at all and if yes - where do I have to look at?
There isn't an EF tool for this from Microsoft, not like they built for SQL projects and dacpacs.
We had a similar need to ensure production schemas didn't differ to the point of runtime errors so I wrote one:
https://github.com/reckface/EntityFramework.Verify
It's covered in this Code review question. It doesn't generate any SQL to fix the differences, but does notify you of the differences.
try using Devart Entity Developer it's easy to use and let you compare EF models with Database. I've used it for over 3 years and works like charm.

Schema generation using Hibernate Vs Manual schema generation

I have worked on projects that involved creation of schema by hand coding sql scripts. Then we have used hibernate to do DML related activites.
Now, I am starting a project that involves extensive database entities creation and I was wondering if it is a good idea to use Hibernate itself to generate the entities. In other words, is hibernate capable of handling all possible DDL related scenarios, especially the ones that are complex in nature. Or, is it advisable to hand code the DDL sql scripts and use Hibernate for DML related tasks.
Thanks for your inputs.
No, Hibernate isn't able to handle all possible situations (synonyms, tablespaces, and all sorts of various things can't be handled by Hibernate).
I would only consider using Hibernate (to handle the schema creation and updates) for a quick and dirty POC. Otherwise, SQL scripts or Liquibase are your friends. You'll need them once you have a database in production that you need to migrate anyway.

Which ORM frameworks will build and execute the SQL DDL for you?

Entity Framework Code First will build the database for you if it doesn't exist and structure it based on your mapping objects. I believe Roundhouse will do the same thing with Fluent Mapping files using NHibernate.
Are there any other ORM's (or tools like Roundhouse) that will take care of all your SQL DDL creation and execution?
NHibernate does not need Fluent Mappings to generate database schema. This feature is built into the NHibernate core:
new SchemaExport(_configuration).Execute(false, true, false);
In my experience however this is mostly useful for in-memory integration tests or initial rollouts. Production databases need to be upgraded. If you stick around, then you will need to add and remove columns, tables and foreign keys without affecting data. There is a continuity and versioning aspect to it. NHibernate only knows your current mapping. It does not know for example that 2 months ago you stored your customer first and last name in column called "CustomerName" and then you decided to split this into two columns "FirstName" and "LastName" (which is probably the most primitive change that can be made). NHibernate job is to map your current schema to objects, not to remember data modeling choices from few years ago.
In my experience there is no magic tool that will write upgrade scripts, they have to be written manually or at least reviewed by developer. Tools can provide you a framework for executing these scripts, like RoundhouseE. Scott Allen has an excellent series about 'forward-only, run-once' approach.
hibernate does if you set hbm2ddl.auto to create or create-drop or update in the config file. Using Java that is, I assume its the same for nHibernate.
If an ORM does not do ddl, there is not much point in having it, well its a key feature at least. Imho.

How do you create a database from an EDM?

How do you create a database from an Entity Data Model.
So I created a database using the EDM Designer in VisualStudio 2008, and now I want to generate the SQL Server Schema to create storage in SQL Server.
From what I understand you are not just supposed to use EDM as a "pretty" database designer, in fact EDM does not depend on a specific storage layer. It tries to abstract that part for the developer. There are design schemas (CSDL) and storage schemas (SSDL). Anyway, don't mean to lecture you. ;)
There is EDM Generator, which you use to create models and class, etc.. For a DDL kind of export, I've never done that but what I did was map my EDM to an existing database, which was easier for me to get started.
There is a great tutorial on MSDN, which details step by step instructions on how to go about using an existing database, but also touches the how to start from scratch approach.
http://msdn.microsoft.com/en-us/magazine/cc163286.aspx
The Feature "Generate Database Schema from Model" is scheduled for a future release of Entity Framework. V1 does'nt support schema generatiorn based on EF models.
I believe the other answers implied this, but just to be explicit - use SSMS (or whatever equivlent if you're a brave sole and not using SQL Server provider) to design the DB layout and then suck that into EDM - and then apply application changes as necessary to the model.
I spent about an hour trying to do it your way first (leftover habit from some other Java ORM tools) - I eventually gave up and now do it the 'Right Way' (tm)
Eventually it would be nice (as JRoppert indicated) to have the generate databse schema from model feature - then you could get your DDLs for various DB flavours automagically.
Generating databases from model is a feature planned for vNext of Entity Framework.
Check out this blog post of Entity Framework Design explaining the planned features for database generation from a model.
What you must do right now is either 1) generate the database by hand, or 2) parse the CSDL file and write your own generator. I think option 1) is probably a better option.
Avilable in EF 4:
http://blogs.msdn.com/b/efdesign/archive/2008/09/10/model-first.aspx

How can I generate "migration" DDL from NHibernate mapping files?

I'm using NHibernate 2 and PostgreSQL in my project. SchemaExport class does a great job generating DDL scheme for database, but it's great until the first application.
Is there any way to generate "migration" DLL (batch of "ALTER TABLE"'s instead of DROP/CREATE pair) using NHibernate mapping files?
Look into SchemaUpdate. Very similiar API as SchemaExport but it only creates migrations.
While SchemaUpdate very much answers my needs, it still has several problems. For example it refuses to put a new restriction on existing database column even if it's not gonna conflict with existing data.
I'm going froward to extend SchemaUpdate a little bit or, if fail, switch to one of that hand driven migration tools (for example Rails one).