C# 从另一个项目中的1个项目运行代码优先迁移

C# 从另一个项目中的1个项目运行代码优先迁移,c#,entity-framework,entity-framework-6,entity-framework-migrations,C#,Entity Framework,Entity Framework 6,Entity Framework Migrations,我有一个不寻常的问题,我会尽力解释 我有一个WebAPI项目,它利用实体框架迁移。使用下面的设置,这一切都可以很好地工作。webapi项目引用了my Repositories项目,其中包含MyContext、我的所有迁移文件和所有迁移配置 WebAPI项目中的Global.asax.cs Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Repositories.Migrations.Configur

我有一个不寻常的问题,我会尽力解释

我有一个WebAPI项目,它利用实体框架迁移。使用下面的设置,这一切都可以很好地工作。webapi项目引用了my Repositories项目,其中包含
MyContext
、我的所有迁移文件和所有迁移配置

WebAPI项目中的Global.asax.cs

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Repositories.Migrations.Configuration>());
在这个单独的应用程序中,这是一个windows窗体应用程序,我基本上只做了对我的存储库项目(以及所需的其他相关项目,例如模型)的引用,并称为上文详述的
UpdateDatabase
方法

因此,表单应用程序中的代码可以简化为:

private void btnSubmit_Click(object sender, EventArgs e)
{  
    MyContext.UpdateDatabase(newConnectionString);
}
以前有没有人有过开发类似产品的经验,或者我是不是走错了方向?我试图实现的目标似乎很简单,但还不完全实现

public static MyContext UpdateDatabase(string _connectionString)
    {
        try
        {
            var context = new MContext(_connectionString);
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
            DbMigrator migrator = new DbMigrator(new Configuration(new DbConnectionInfo(context.Database.Connection.ConnectionString, "System.Data.SqlClient")));
            migrator.Update();
            return context;
        }
        catch (Exception err)
        {
            logger.Error("Error updating database", err);
            throw;
        }
    }
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
private void btnSubmit_Click(object sender, EventArgs e)
{  
    MyContext.UpdateDatabase(newConnectionString);
}