Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been programming recently with Entity Framework 4.1 Code First and am loving it for development, but with only an end plan and a rapidly changing feature list, I am constantly modifying the Class/Database to meet the applications needs.

In development, there is no live data and I can easily just delete the entire database so it is recreated with the new schema however, obviously, when live - this is very bad!

The only solutions I can see are to either drop the metadata table and manually keep the database in sync or basically drop and reseed.

I personally prefer the first method as I think it will be a lot easier to add a column/table than to recreate and migrate data, but, unless I have missed something, this is completely moving away from Code First.

So the question really is, is Code First just about the initial development and what is a good strategy for managing EF for a production environment?

share|improve this question
    
Been meaning to ask this for a few days, wasn't sure if it was best here or on Stack Overflow... –  wilhil May 13 '11 at 11:12
add comment

3 Answers

up vote 13 down vote accepted

My opinion is that code first's automatic database creation is only for development. I answered similar questions on Stack Overflow where I described both how to upgrade the database and why is automatic functionality bad in production:

Upgrading the database is semi-manual task. There should be no automatic untested magic behind - moreover EF 4.1 currently doesn't have any such magic available (there is only some presentation about features ADO.NET team is working on).

You can also check this question to more better understand how are web sites upgraded.

share|improve this answer
    
Hello again! -you are quick on EF questions! :) ... Don't know where I would be without you! –  wilhil May 13 '11 at 11:49
    
A few months on and my program is pretty finished... I am marking this as answer, but, I was wondering if anything has changed / are there any resources that can help? –  wilhil Aug 22 '11 at 21:12
2  
First public preview of "Migrations" was released. blogs.msdn.com/b/adonet/archive/2011/07/27/… –  Ladislav Mrnka Aug 22 '11 at 21:20
    
Thanks, looking at that now! I loved developing with Code First, but, I am so nervous/worried about changing later! –  wilhil Aug 22 '11 at 21:23
    
How do you handle case where StoredProcs or Views are created directly in the DB for other purposes e.g. reporting that are not used by the application. We would need to know which SPs are affected by a schema change in code first. –  Pratik Jan 12 '12 at 21:58
add comment

Maintain upgrade scripts.

In the database itself, maintain a table where a record is held with the version of the schema.

When you application start, it detects the version against the version supposed to be used by the binaries. If it differs, it execute (or ask the user to) the upgrade scripts.

Don't forget to backup the database first.

share|improve this answer
    
Next step: you are fired ;) Database updates should not ahppen automatically without a backup first - and possibly in downtime, not when ONE USER runs a newer version. Your approach is perfect - to shut down largeer deployments with tons of errors. –  TomTom Feb 4 at 12:20
add comment

The question is somehow flawed in that it makes connections between the programming model and the runtime environment where there is none.

Code first is primarily a development speed driver and is not really connected to the runtime system.

In production you will properly have a configuration setting that denies the runtime the possibility of deleting/updating the db model.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.