简体   繁体   English

实体框架代码首先 - 初始代码迁移不起作用

[英]Entity Framework code first - initial code migration not working

I am trying to run migrations from code. 我正在尝试从代码运行迁移。 I created my models and enabled-migrations and then added initial migration, this initial migration contains all the create tables etc 我创建了模型和启用迁移,然后添加了初始迁移,这个初始迁移包含所有创建表等

public partial class Initial : DbMigration
 {
     public override void Up()
     {
        CreateTable(..........
     }

 public override void Down()
    {
           DropTable(...........
     }
}

I then tried the Update-Database command from the visual studio which works fine, creates the database and runs the initial migration. 然后,我尝试了Visual Studio中的Update-Database命令,该命令工作正常,创建数据库并运行初始迁移。

I then delete the database from the Sql Studio. 然后我从Sql Studio中删除数据库。 Then I run the console app that calls the Migration Manager class 然后我运行调用Migration Manager类的控制台应用程序

// MigrationManager class

    public static bool PerformMigration(string migrationId)
    {
        bool success = false;
        try
        {
            DbMigrationsConfiguration<MyDbContext> config = new Configuration();
    ....
            DbMigrator migrator = new DbMigrator(config);
            migrator.Configuration.AutomaticMigrationsEnabled = false;
            if (string.IsNullOrEmpty(migrationId))                    
                migrator.Update(); --> fails saying pending migration
            else
                migrator.Update(migrationId);

            success = true;
        }
        catch (Exception e)
        {
            success = false;
            LastException = e.Message;
        }

        return success;
    }

The Update() fails with the following error: Update()失败并出现以下错误:

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. 将DbMigrationsConfiguration.AutomaticMigrationsEnabled设置为true以启用自动迁移。

//Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(WorkflowConfigurationDbContext context)
    {
        SeedData(context);
    } 
    private void SeedData(){...}    

}

public class MyDbContext : DbContext
{
    public MyDbContext()
    {            
    }        

    public DbSet....

    }

When I step through the Update() call, it goes into Configuration() constructor and MyDbContext() constructor but fails after that, it seems like its not trying the Initial migration at all. 当我逐步执行Update()调用时,它进入Configuration()构造函数和MyDbContext()构造函数,但在此之后失败,似乎它根本没有尝试初始迁移。

Make sure the database initialization strategy is correct in your EF context's constructor, like: 确保EF上下文的构造函数中的数据库初始化策略正确,如:

 public partial class YourContext: DbContext
 {
     static YourContext()
     {
         Database.SetInitializer<YourContext>(new CreateDatabaseIfNotExists<YourContext>());
     }
 }

This is executed the first time the database is accessed. 这是在第一次访问数据库时执行的。

EDIT: A second issue may be about security: does the user that is executing the migration have the required permissions? 编辑:第二个问题可能与安全性有关:执行迁移的用户是否具有所需的权限?

Found the issue, it was incorrect namespace. 发现问题,它是不正确的命名空间。

DbMigrationsConfiguration<MyDbContext> config = new Configuration();
config.MigrationsNamespace = "Correct Namespace";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM