[英]Entity Framework 4.3.1 always run all migrations on Update-Database
我使用Add-Migration
創建了初始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),但我不確定問題是否存在。 也許我錯過了一些微不足道的事情?
與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項目不同。
值得理智檢查!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.