Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does EF 4 code first handle schema changes in a production environment?

Traditionally I have always written my sql scripts by hand so they are nice and clean (I'm not a fan of the generated ones) and release to release, I provide a fresh install script and a migration script from the previous version that creates new tables, alters existing tables etc. This is all pretty standard.

I haven't really had much time to play around with EF 4 code first but am quite interested to use it if it is actually viable in a production environment.

Say you have a code first approach where a database will automatically be created if one does not exist. What happens if you release a new version of the software which has schema/model changes. Is EF smart enough to update the database schema to match the updated EF model?

Scenario

  1. Client installs asp.net MVC website on their server. Upon first run, a fresh database is created
  2. Client uses website for a while and the database gets populated with some data
  3. Meanwhile a new version of the website is released and the EF model has changed
  4. Client downloads new version, deploys website and points to existing database

Is code first only useful for initial deployment, or is it smart enough to update an existing database release to release like this?

like image 252
Joshua Hayes Avatar asked Nov 18 '10 03:11

Joshua Hayes


People also ask

How do I code my first migration to an existing database?

Run the Add-Migration InitialCreate command in Package Manager Console. This creates a migration to create the existing schema. Comment out all code in the Up method of the newly created migration. This will allow us to 'apply' the migration to the local database without trying to recreate all the tables etc.

How do you deal with schema changes in EF Core?

With EF Core, you deal with database schema changes by using migrations. When you first create the database, you create a migration that contains the initial definition of the database. As you make schema changes, you add new migrations and apply them on top of the existing migrations.

Is there a way to tweak the generated code in EF?

But a command that will “tweak” the generated code is on the EF Core roadmap and should be out sometime in 2018. 1. EF Migrations can be difficult EF migrations work well for a simple system, but they start to get complex when used on a live site.

How to indicate default schema at runtime in Entity Framework Code-first?

With fluent mappings in Entity Framework code-first you can indicate the default schema at runtime. This is one statement in OnModelCreating in your DbContext subclass, for instance:

What is a database schema change?

Anytime you change the definition of the database – from renaming a column to creating a table – it’s referred to as a database schema change. With EF Core, you deal with database schema changes by using migrations. When you first create the database, you create a migration that contains the initial definition of the database.


2 Answers

As of EF CTP4, your database will be dropped and recreated every time you change your object model (this is not the default convention and you have to explicitly tell EF Code-First to do so by setting a Database Initializer Strategy).

That being said, EF team are actively working on a Database Evolution (aka Migrations) Solution that exactly addresses your scenario: A solution that will evolve the database schema as your object model changes over time which essentially tries to alter the database to be back in sync with your model instead of recreating it.

As per EF team, this feature will be available as part of EF next version that is on track to be released on the 1st quarter of 2011.

like image 53
Morteza Manavi Avatar answered Oct 19 '22 23:10

Morteza Manavi


The ability to create a database is just one feature of Code First - and it's an optional feature. You don't have to use this feature at all. In fact, Scott Gu has an entire blog post dedicated to using Code First with an existing database.

Until the Database Migrations are released, you have to come up with another strategy and that strategy will simply be managing your ALTER TABLE scripts as you traditionally would have. So when you deploy a new version, you run your ALTER script and deploy the code that contains the changes to the model.

Having said all that, you get more options in Code First than simply dropping and recreating your database every time (that is just one option). You can also set the initializer to only recreated the database if the model changes. You can also set the initializer to never run at all (in the case where you're manually managing changes to the database). This post will give you more information on EF database initializers.

like image 39
Steve Michelotti Avatar answered Oct 20 '22 00:10

Steve Michelotti