EF Data Migration


EF automatic data migration is always a failure. At the beginning, it is all right. It can migrate automatically and fail to know why it always fails.

I can only move it manually.


This time, attributes are added to a class, resulting in the failure of automatic migration of other tables.

Previously, data was lost due to the inconsistent automatic migration between the website and the server version, and later the data migration was shut down.

Start from scratch and do it again.

Reference resources: https://www.cnblogs.com/libingql/p/3330880.html

Step 1: Activate

Enable-Migrations -EnableAutomaticMigrations


More than one context type was found in the assembly 'DAL'.
To enable migrations for 'DAL.BaseDataDb', use Enable-Migrations -ContextTypeName DAL.BaseDataDb.
To enable migrations for 'DAL.EngineDb', use Enable-Migrations -ContextTypeName DAL.EngineDb.
To enable migrations for 'DAL.LocationDb', use Enable-Migrations -ContextTypeName DAL.LocationDb.
To enable migrations for 'DAL.LocationDbLite', use Enable-Migrations -ContextTypeName DAL.LocationDbLite.
To enable migrations for 'DAL.LocationHistoryDb', use Enable-Migrations -ContextTypeName DAL.LocationHistoryDb.

Because I have multiple databases, I need to develop databases.

Enable-Migrations -EnableAutomaticMigrations -ContextTypeName DAL.LocationDb

Yes, a Migrations folder is automatically created:

Change the name and namespace

internal sealed class Configuration : DbMigrationsConfiguration<DAL.LocationDb>
        public Configuration()
            AutomaticMigrationsEnabled = true;
            MigrationsDirectory = @"LocationDbMigrations";

        protected override void Seed(DAL.LocationDb context)
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.

Step 2: Initialization

Add-Migration InitialCreate


No MigrationSqlGenerator found for provider 'MySql.Data.MySqlClient'. Use the SetSqlGenerator method in the target migrations configuration class to register additional SQL generators.


public Configuration()
            AutomaticMigrationsEnabled = true;
            MigrationsDirectory = @"LocationDbMigrations";

            this.SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());

Run again:

Create a file of _InitialCreate.cs

namespace DAL.LocationDbMigrations
    using System;
    using System.Data.Entity.Migrations;
    public partial class InitialCreate : DbMigration
        public override void Up()
            AddColumn("dbo.Personnels", "TargetType", c => c.Int(nullable: false));
            AddColumn("dbo.Personnels", "TargetModel", c => c.String(maxLength: 512, storeType: "nvarchar"));
        public override void Down()
            DropColumn("dbo.Personnels", "TargetModel");
            DropColumn("dbo.Personnels", "TargetType");

Last step: update

Update-Database -Verbose

All right

There are two more attributes in the database.


I want to deploy the program to the client computer and run it automatically to modify the database.

Restore the database, pack and run on the basis of the migration file just created.

The database can also be modified.

Okay, keep writing.

Tags: Database MySQL SQL

Posted on Wed, 02 Oct 2019 18:30:21 -0400 by metalblend