2016-11-08 120 views
3

我的自動遷移在嘗試更新數據庫時不斷給我提供此錯誤。實體框架影響遷移歷史記錄位置的自動遷移

影響遷移 歷史系統表(如默認架構更改)的位置

自動遷移是支持不 。請將基於代碼的遷移用於影響遷移歷史記錄系統表的位置的操作 。

這裏是我的代碼:

[DbConfigurationType(typeof(AlvinCMSExtension.Migration.AlvinCMSCustomHistoryConfiguration))] 
public class AccountDBContext : DbContext 
{ 
    public AccountDBContext() 
     : base("DefaultConnection") 
    { 
     Database.SetInitializer(new MigrateDatabaseToLatestVersion<Alvin_CMS.Models.AccountDBContext, Alvin_CMS.Migrations.AccountDBContext.Configuration>()); 
    } 

    public DbSet<UserProfile> UserProfiles { get; set; } 
    public DbSet<Membership> Memberships { get; set; } 
    public DbSet<Role> Roles { get; set; } 
    public DbSet<UsersInRole> UsersInRoles { get; set; } 
    public DbSet<OAuthMembership> OAuthMemberships { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 

     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()); 
     string query = "select schema_name()"; 

     if (con.State == ConnectionState.Closed) 
      con.Open(); 

     SqlCommand com = new SqlCommand(query, con); 
     var x = com.ExecuteScalar(); 

     if (con.State == ConnectionState.Open) 
      con.Close(); 

     modelBuilder.HasDefaultSchema(x.ToString()); 
    } 
} 

internal sealed class Configuration : DbMigrationsConfiguration<Alvin_CMS.Models.AccountDBContext> 
{ 
    public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
     MigrationsDirectory = @"Migrations\AccountDBContext"; 
     //SetHistoryContextFactory("System.Data.SqlClient", (conn, schema) => new AccountHistoryContext(conn, schema)); 
    } 

    protected override void Seed(Alvin_CMS.Models.AccountDBContext context) 
    { 
     // This method will be called after migrating to the latest version. 

     // You can use the DbSet<T>.AddOrUpdate() helper extension method 
     // to avoid creating duplicate seed data. E.g. 
     // 
     // context.People.AddOrUpdate(
     //  p => p.FullName, 
     //  new Person { FullName = "Andrew Peters" }, 
     //  new Person { FullName = "Brice Lambson" }, 
     //  new Person { FullName = "Rowan Miller" } 
     // ); 
     // 
    } 
} 

public class CustomHistoryContext : HistoryContext 
{ 
    public CustomHistoryContext(DbConnection dbConnection, string defaultSchema) 
     : base(dbConnection, defaultSchema) 
    { 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.HasDefaultSchema("dbo"); 
     modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "dbo"); 
     //modelBuilder.Entity<HistoryRow>().ToTable(tableName: "__MigrationHistory", schemaName: "dbo"); 
    } 
} 

public class AlvinCMSCustomHistoryConfiguration : DbConfiguration 
{ 
    public AlvinCMSCustomHistoryConfiguration() 
    { 
     this.SetHistoryContext("System.Data.SqlClient", 
      (connection, defaultSchema) => new CustomHistoryContext(connection, "dbo")); 
    } 
} 

我可以做遷移,而與其他數據庫上下文的任何問題,但只有這個AccountDBContext錯誤總是發生。這個錯誤的原因是什麼?

+0

一個不好的解決方案是刪除數據庫,然後在DB中沒有任何重要的情況下應用遷移。 –

+0

我在新創建的數據庫上做了這個,結果是一樣的 –

回答

1

錯誤是因爲您已經定義了自定義模式名稱。如果您想使用自定義模式名稱,則不能使用自動遷移。如果您想使用自動遷移,請在OnModelCreating內刪除對HasDefaultSchema的呼叫。

Here is如何啓用Code First Migrations。