Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate and code first

Do you use SchemaExport and SchemaUpdate in real applications? Initially, you create model and then generate schema? Does it work? Or, you use it only for tests...

Usually, I create db (using visual studio database project) and then mappings and persistent classes or EF entities using designer. But now, I want to try code first approach with Fluent NHibernate.

I have researched SchemaExport and SchemaUpdate and found some issues. For example, update doesn't delete db objects, creates not null columns like nullable if table exists, doesn't generate primary key on many-to-many tables and so on. It mean that I have to recreate db very often. But, what's about data? And, how to deploy changes to production db and so on...

I want to know do you really use code first and SchemaExport(SchemaUpdate) in your applications? May be you can give me some advices...

like image 613
Andy Avatar asked Nov 18 '10 15:11

Andy


2 Answers

I use SchemaUpdate in production. It is safe precisely because it never does destructive operations like deleting columns. However, it is not a comprehensive solution for updating your database. If you use it you will still have to supplement it with script to update your schema to do things like deleting (as you mention), indexes, changing column type, adding table data, etc. But SchemaUpdate covers the 90% case for me.

The only downside I've discovered is that over time it seems to occasionally add duplicate foreign-key constraints to my table.

One more thing: you should run SchemaUpdate manually from a build tool, not your app itself. It is not safe to give your application the rights to modify your db schema!

like image 130
Gabe Moothart Avatar answered Sep 27 '22 21:09

Gabe Moothart


I use SchemaUpdate/SchemaExport for rapid evolution of my model, but they are not a replacement for a database migration tool. As you mention, data cannot be migrated in a sensible manner in many cases. The tool does not have enough context. (e.g. How can you automatically migrate a FullName column to FirstName/LastName?) I answered a similar question here where I discuss db migration tools in the context of NHibernate.

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

like image 21
James Kovacs Avatar answered Sep 27 '22 19:09

James Kovacs