实体框架4.3.1始终在更新数据库上运行所有迁移

本文关键字:数据库 运行 迁移 更新 框架 实体 | 更新日期: 2023-09-27 18:27:53

我已经用Add-Migration创建了一个初始迁移。当我在一个空数据库上运行Update-Database时,它会创建所有表,包括在__MigrationHistory表中添加一个条目。

现在我再次运行Update-Database只是为了测试,而不是"未检测到更改",我得到的是:

PM> Update-Database -Verbose -Project testProject.Web
Using StartUp project 'testProject.Web'.
Target database is: 'testProject_dbo' (DataSource: localhost, Provider: Devart.Data.MySql, Origin: Explicit).
Applying explicit migrations: [201203151243164_Start].
Applying explicit migration: 201203151243164_Start.
CREATE TABLE attachments ( 
 ...table data...
)
Table 'attachments' already exists
Table 'attachments' already exists

更新似乎不知道当前的DB状态。唯一的解决方案是删除所有表并更新。如果我添加更多的迁移,这也会起作用。

正如您所看到的,我使用的数据库提供程序与平时不同(Devart.Data.Mysql),但我不确定问题是否存在。也许我错过了一些琐碎的事情?

实体框架4.3.1始终在更新数据库上运行所有迁移

与DevArt沟通后,问题得到解决。我从未在运行Enable-Migrations时生成的Configuration类中调用过IgnoreSchemaName解决方法。总之,这是使它最终发挥作用的类:

internal sealed class Configuration : DbMigrationsConfiguration<YourDbContext>
{
    public Configuration()
    {
        // Because the Package Manager Console (NuGet) instantiates YourDbContext with the empty constructor,
        // a custom connection must be specified. Based on http://www.devart.com/blogs/dotconnect/?p=5603
        // Note that the MySqlProviderFactory must also be present in Web.config or App.config in the *startup project*
        // for this to work! Configuration example:
        /*
          <system.data>
            <DbProviderFactories>
              <clear />
              <remove invariant="Devart.Data.MySql" />
              <add name="dotConnect for MySQL" invariant="Devart.Data.MySql" description="Devart dotConnect for MySQL" type="Devart.Data.MySql.MySqlProviderFactory, Devart.Data.MySql, Version=6.30.196.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
            </DbProviderFactories>
          </system.data>
        */
        // Apply the IgnoreSchemaName workaround
        MySqlEntityProviderConfig.Instance.Workarounds.IgnoreSchemaName = true;
        // Create a custom connection to specify the database and set a SQL generator for MySql.
        var connectionInfo = MySqlConnectionInfo.CreateConnection("<Your ConnectionString>");
        TargetDatabase = connectionInfo;
        SetSqlGenerator(connectionInfo.GetInvariantName(), new MySqlEntityMigrationSqlGenerator());
        // Enable automatic migrations if you like
        AutomaticMigrationsEnabled = false;
        // There is some problem with referencing EntityFramework 4.3.1.0 for me, so another fix that needs
        // to be applied in Web.config is this:
        /*
          <runtime>
            <assemblyBinding>
              <!-- This redirection is needed for EntityFramework Migrations through the Package Manager Console (NuGet) -->
              <dependentAssembly>
                <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
                <bindingRedirect oldVersion="4.3.0.0" newVersion="4.3.1.0" />
              </dependentAssembly>
            </assemblyBinding>
          </runtime>
        */
        // After these Web.config additions, running migrations in Package Manager Console should be as easy as:
        // Update-Database -Verbose -ProjectName Your.MigrationsProject
        // Creating new migrations:
        // Add-Migration -Name MigrationDescription -ProjectName Your.MigrationsProject
    }
}

之后,我再次清空数据库以生成正确的迁移历史条目,一切都很好。DevArt提供了有关配置的更多详细信息。

我也有同样的奇怪行为,但在我的情况下,它要简单得多:代码合并将我的启动项目重置为默认值,而不是包含迁移的项目。

直到我尝试了-Verbose标志,它明确指出我的Startup项目与Package Manager中配置的NuGet项目不同,我才注意到这一点。

值得进行理智检查!