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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With