-1

我加了一些實體我context.And然後我將它們遷移爲低於我的工具實體代碼首先遷移嘗試刪除舊錶

using (var db = new TourismContext()) 
       { 
        if (db.Database.CompatibleWithModel(true)) 
         return; 

        var initializer = new MigrateDatabaseToLatestVersion<TourismContext, TourismContextConfiguration>(); 

        initializer.InitializeDatabase(db); 
//and other code..... 

拖放我刪除它們從我的context.And表它的作品。它是兼容的。但20分鐘後它說不兼容。他想刪除一些表,但這個表不存在 我該怎麼辦?

儘管我從上下文中刪除表並遷移它(遷移工具從數據庫中刪除表)爲什麼要一次又一次地刪除它們?遷移要刪除的表在數據庫中不存在,因爲我遷移它們因此遷移工具會將它們刪除。在遷移過程中,哪些表信息會丟失?

這是我__MigrationHistory

SELECT TOP 1000 [MigrationId] 
     ,[Model] 
     ,[ProductVersion] 
     ,[CreatedOn] 
    FROM [TOURISM_new1].[dbo].[__MigrationHistory] 

這裏沒有滴速表信息

這裏是我的移民tool.There是另外兩個buton.one顯示scritp它會顯示執行腳本(Script按鈕)是micration buton.it遷移

private void MigrateButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       using (var db = new TourismContext()) 
       { 
        if (db.Database.CompatibleWithModel(true)) 
         return; 

        var initializer = new MigrateDatabaseToLatestVersion<TourismContext, TourismContextConfiguration>(); 

        initializer.InitializeDatabase(db); 

        foreach (Constants.SecurityFeatureIdentifier securityFeatureIdentifier in Enum.GetValues(typeof(Constants.SecurityFeatureIdentifier))) 
        { 
         if (db.SecurityFeatures.All(sf => sf.SecurityFeatureIdentifierID != (int)securityFeatureIdentifier)) 
         { 
          db.SecurityFeatures.Add(new SecurityFeature { SecurityFeatureIdentifier = securityFeatureIdentifier }); 

          db.SaveChanges(); 
         } 
        } 
       } 

       statusLabel.Text = "Compatible"; 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

     private void ScriptButton_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       using (var db = new TourismContext()) 
       { 
        if (db.Database.CompatibleWithModel(true)) 
        return; 

        var migrator = new DbMigrator(new TourismContextConfiguration()); 

        var scriptor = new MigratorScriptingDecorator(migrator); 

        scriptControl.Text = scriptor.ScriptUpdate(null, null); 

        foreach (Constants.SecurityFeatureIdentifier securityFeatureIdentifier in Enum.GetValues(typeof(Constants.SecurityFeatureIdentifier))) 
        { 
         if (db.SecurityFeatures.All(sf => sf.SecurityFeatureIdentifierID != (int)securityFeatureIdentifier)) 
         { 
          db.SecurityFeatures.Add(new SecurityFeature { SecurityFeatureIdentifier = securityFeatureIdentifier }); 

          db.SaveChanges(); 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.ToString()); 
      } 
     } 

回答

0

您需要讓您的數據庫模型與您的代碼第一個模型同步。您可以將遷移的Up()方法中過時的代碼註釋掉並執行更新數據庫,也可以刪除待定遷移並使用更新數據庫執行Add-Migration MyBaseline -IgnoreChanges。現在你應該兼容,直到你改變你的模型。

https://msdn.microsoft.com/en-us/data/dn579398.aspx?f=255&MSPPError=-2147217396#option1

+0

我怎麼能同步?我沒有向上()method.Should我加起來()上下文? – user1688401

+0

Up()方法由遷移自動創建。你提到你的遷移試圖刪除不存在的表你在哪裏看到這個? –

+0

我添加了遷移工具buton.in,它顯示腳本之前執行it.i更新我的問題。你會看到腳本生成buton var scriptor =新MigratorScriptingDecorator(migrator); scriptControl.Text = scriptor.ScriptUpdate(null,null); – user1688401