EF 7 Migrating to an Existing Database

I am working on a web project using ASP.Net 5 and EF7.

I imported all the tables from the existing database into my models in my project. However, I have problems with migrations.

I created the initial migration, made some changes to a specific entity, created another migration after the changes that I made, and now I want to apply the changes to the database.

After executing this command below:

dnx ef database update [Migration]

dnx tries to apply the “initial” migration with all entities that are already in the database, and this causes an error, as shown below:

{The database already has an object named ['EntityName']. }

Could you advise how to migrate to an existing database?

Thanks Saeed

+4
3

EF6 -IgnoreChanges, Up(). EF 7 (EF Core), .

- Up() , . .

+7

, (EF7) ( ) - "Initial" () , __EFMigrationHistory .

sql insert into ... select . - , DropIndex CreateIndex, DropIndex , ( , pre -EF7).

- ( EF6) , .

+3

2 EFCore, Google !

?

10 , , . , " 'tableName'." , , , , ". ['EntityName' ] ." .

?

  • __EFMigrationsHistory ( )
  • :

: Data, , .

Scaffold-DbContext " " Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data​​p >

  1. :

: (OldDataBaseContext , 2)

Add-Migration initial -Context OldDataBaseContext

  1. Up 3
  2. :

: (OldDataBaseContext , 2)

- - OldDataBaseContext

  1. Delete the data folder created in step 2
  2. Go to the snapshots and the initial migration classes and change the remote context from the data folder to the main context that exists in your database project (just fix it for assembly).
  3. Run:

Note before starting: add a hyphen for the main context with new database changes

Add-Migration newUpdate

  1. Run:

Update database

Hope this helps someone.

+2
source

Source: https://habr.com/ru/post/1628285/


All Articles