Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

entity framework code first migration keep existing data

I am using EF 6.1 and I use code first approach with an existing database with data in a production environment.

Is it possible at all to migrate model changes and keep the existing customer`s data?

like image 757
Pascal Avatar asked May 01 '14 20:05

Pascal


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 use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add. This will open the Entity Data Model wizard as shown below.

Is it possible to create a code first model based on the existing database?

Show me the code! First, right click on the folder where you want your model to go, select Add -> New Item, then select ADO.NET Entity Data Model. Click Add, and select "Code First from Database", then click Next. On this next screen, pick your connection information and name the connection string, then click Next.


1 Answers

Yes, however, it can be complicated depending on specific case (on complexity of changes):

Overview: https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/existing-database

Read this: https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/index

And then this: https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/teams

Update: updated the links based on redirects.

like image 137
Danny Varod Avatar answered Oct 20 '22 14:10

Danny Varod