Reputation: 3212
I developed a website using EF 4.1 code first,Mvc3 ,Sql Sever 2008 r2, and deployed it.
The database on the host got filled with critical data and it still grows. Now I want to add new columns to different tables and also add new tables. Even if I back up my SQL and bring it to the development environment and using base.seed() or even create a script using SQL server management I will lose data and I have tried different way and I couldn't find a way which I wouldn't lose data.
I looked at code first migration and it didn't solve my problem. My team added some tables to database using SQL server management and if I use code first migration it wont pick the changes that occurs manually. someone suggested that using reverse engineering tools that would create code first from database but this also makes many unwanted code.
What is the best way or best practices for changing database using code first approach?
Upvotes: 2
Views: 4894
Reputation: 364279
Migrations will help you. You just need to script current database and recreate it in your environment. Then you need to add empty initial migration to use your current database as a starting point. You will then add all new tables and columns and let migrations do their job. Sure there can be problems because you have manually changed the database in the production but that is your team's failure in the first place because it violates code first development approach. If this was supposed to happen you should not use code first approach.
As alternative simply develop new version of your application with new tables and columns, create database from your application in your development environment and use database tools for schema compare either in VS 2010 (Premium or Ultimate) or in another commercial tool like Red Gate Compare. This will be able to create diff SQL script for upgrading old DB schema to a new one.
Upvotes: 3
Reputation: 33865
For EF 4.1 you have the Code First Migrations available as a Nuget-package, that let's you migrate you database as your models change.
As of EF 4.3, Migrations is now included in Entity Framework.
The ADO.NET team has blogged about how to get started with EF 4.3 Migrations.
Upvotes: 2