EF 7 Migration to Existing Database

后端 未结 3 955
野趣味
野趣味 2021-02-06 00:11

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

I have imported all the tables from existing database onto my models in my project. However, I am having problems

相关标签:
3条回答
  • 2021-02-06 01:02

    In EF6 you would run a migration with the -IgnoreChanges flag and it would take a snapshot of the model without any Up() code. This is missing from EF 7(EF Core) as indicated here.

    The workaround for now is delete or comment out the code for existing database objects from the Up() code of the migration and then update-database. Subsequent migrations will then include only the changes.

    0 讨论(0)
  • 2021-02-06 01:08

    After 2 days I found a way for EFCore that is not in google and internet!

    How My steps works?

    When you have a database with 10 table and you have data in tabales that you don't want to clear's data.and after this you will create new models in your code first and runnig to existing database and you will get error "Invalid object name 'tableName'." for query to new tables and you want to create migrations and do update it to existing database but first migration will create all queries for old and new tables if you run update-database you will get "There is already an object named ['EntityName'] in the database." for your initial migration.

    How fix it?

    1. Delete all migrations and snapshot in you database project
    2. Delete __EFMigrationsHistory table in your existing database (if exist)
    3. Run in Package manager console:

    Note before run: This code will create new context and models of your existing database to Data folder so don't forgot to check you have not Data folder in your project.

    Scaffold-DbContext "your connection string" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Data

    1. Run in Package manager console:

    Note before run: Create first migration for initial database with Data folder context (OldDataBaseContext is inside of your Data folder that created by step 2)

    Add-Migration initial -Context OldDataBaseContext

    1. Delete all code in Up method inside of created initial migaration in step 3
    2. Run in Package manager console:

    Note before run: Update databse with Data folder context (OldDataBaseContext is inside of your Data folder that created by step 2)

    Update-Database -Context OldDataBaseContext

    1. Delete data folder that created in Step 2
    2. Go to snapshot and initial migration classes and change the deleted context from Data folder to main context that is exist in your database project (just fix it for build)
    3. Run:

    Note before run: Add migration for main context that have new database changes

    Add-Migration newUpdate

    1. Run:

    Update-Database

    I Hope this help someone.

    0 讨论(0)
  • 2021-02-06 01:15

    If you are strongly believe, that new (EF7) database schema will match your old database schema (including index and key names) - you may run 'Initial' migration to empty (temporary) database and then copy __EFMigrationHistory table from there to your real database.

    Otherwise, I recommend you create empty database using migration and use sql insert into ... select commands to copy data from your old database. Without this, you will receive exceptions when upgrading database later - for example changing index will lead to DropIndex and CreateIndex migration commands, and DropIndex will fail because there is no index with this name (index exist with other, pre-EF7 name).

    In my projects - old (from EF6) and new database schemes are different, and I used second option.

    0 讨论(0)
提交回复
热议问题