4

で初期移行を作成しましたAdd-Migration。空のDBで実行する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

2 に答える 2

3

DevArtと通信した後、問題は解決しました。IgnoreSchemaNameの実行時に生成されたConfigurationクラスの回避策を呼び出したことはありませんEnable-Migrations。要約すると、これは最終的にそれを機能させたクラスです:

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は、構成に関する詳細を提供します。

于 2012-03-16T15:19:18.383 に答える
1

私は同じ奇妙な振る舞いをしましたが、私の場合、それははるかに単純であることがわかりました。コードのマージにより、スタートアッププロジェクトがデフォルトにリセットされました。これは、移行を含むプロジェクトではありませんでした。

フラグを試してみるまで気づきませんでした。-Verboseフラグは、スタートアッププロジェクトがパッケージマネージャーで構成されたNuGetプロジェクトと同じではないことを明示的に示していました。

健全性チェックの価値があります!

于 2014-03-17T20:47:40.410 に答える