2017-05-29 73 views
0

我在應用程序的啓動時運行的所有遷移是這樣的:C#自動遷移從代碼自動遷移?

  var configuration = new DbMigrationsConfiguration<MyContext> 
      { 
       TargetDatabase = new DbConnectionInfo("DataBase"), 
       AutomaticMigrationsEnabled = true, 
       AutomaticMigrationDataLossAllowed = true 
      }; 

      _migrator = new DbMigrator(configuration); 
      _migrator.Update(); 

而且有一些代碼,第一方面:

public class MyContext : DbContext 
{ 
    public DbSet<MyObject> Objects { get; set; } 
} 

public class MyObject 
{ 
    public long Id {get;set;} 

    public byte[] Data {get;set;} 
} 

而且沒有使用任何預先生成(使用EF工具)遷移。起初它已經足夠了,但現在我需要運行一些自定義的SQL代碼來遷移數據庫到最新版本。例如,這一個:

public class MyCustomMigration : DbMigration 
{ 
    public override void Up() 
    { 
     Sql(@" 
      CREATE TABLE Files 
      (
       Id BIGINT IDENTITY PRIMARY KEY, 
       Data VARBINARY(MAX) FILESTREAM 
      ) 
     "); 
    } 

    public override void Down() 
    { 
     DropTable("Files"); 
    } 
} 

我該如何完成這項任務?我嘗試在配置中設置遷移目錄,但沒有成功(沒有任何反應,遷移未觸及)。

回答

0

爲什麼不能通過包管理器控制檯使用內置的遷移管理?

enable-migrations (make sure you have the right project selected - creates a 
        migrations directory) 
add-migration MyFirstMigration 
update-database 

您需要在每次在上下文中更改DbSets中的模型後手動執行此操作。但是這樣它會爲您生成所有遷移Sql,並使用種子方法創建配置類,您可以在運行時使用它更新新的模式:

update-database