简体   繁体   中英

How do you handle database changes in Entity framework

I use codefirst and I release version 1.0 of my app.

Now I change a bunch of table stuff and I want to upgrade 1.0 db of my application with 2.0.

How is this situation typically handled in entity framework?

EF doesn't have anything built-in for generating diff scripts [yet]. EF can drop and recreate the DB but that is not really usable in the real world... ...they seem to have plans for db migrations, but I don't know when/if that will be available ( http://blogs.msdn.com/b/efdesign/archive/2010/10/22/code-first-database-evolution-aka-migrations.aspx ).

If you want to make incremental changes (add/drop tables, columns, constraints, indexes etc), you will have to write SQL scripts that applies the changes needed to go from your version 1 DB to your version 2 db. It can be a good idea to store a db "version number" somewhere in the database to make it easier to apply the right scripts when you have a number of different versions running out "in the wild".

Alternatively there are 3rd party tools that can generate incremental diff scripts ("alter table ... " etc) based on EDMX/db diffs, eg my 'Model Comparer' for EF4: http://huagati.blogspot.com/2010/07/introducing-model-comparer-for-entity.html

One alternative we are using is keep two entity framework model, one with old and one with new. And with help of reflection, you can create a migration algorithm that suites best.

Old EF model can be serialized to XML or any such format. And you can then drop and recreate database and deserialize your EF model from XML and put them back in new DB. However there seems little issue with inserting identity but it can be managed.

But this solution is only for small database for larger databases you will need more advanced solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM