[英]Automatic Migration and manual invocation of Update-Database
即使我已啟用自動遷移,也必須在程序包管理器控制台中手動發出Update-Database
命令嗎?
我正在運行MVC4和EF6。 該解決方案針對.NET 4(不幸的是,我不能更改它)。
編輯 :我知道代碼遷移之間的差異,這將需要我正確地植入現有表。 這個問題更多地是基於以下情況:我添加了一個沒有關系的表,我認為它會運行?
編輯2 :應自動添加的表
============
新表定義
public class InboundFile : Entity
{
[Required]
public String FilePath { get; set; }
}
EF配置文件
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
MigrationsDirectory = @"Migrations";
}
protected override void Seed(Unifi.Context.DBContext context)
{
context.InboundFiles.AddOrUpdate(
x => x.FilePath,
new InboundFile { CreateDate = DateTime.Now, ModifiedDate = DateTime.Now, FilePath = "c:/test", IsDeleted = false }
);
}
數據庫上下文
公共類DBContext:DbContext {
public DBContext()
: base("MyConn")
{
}
public DbSet<User> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<Region> Regions { get; set; }
public DbSet<InboundFile> InboundFiles { get; set; }
}
嘗試將其放在您的Application_Start中
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, Configuration>);
using (var temp = new DbContext()) {
temp.Database.Initialize(true);
}
這將迫使它每次運行應用程序時都運行update-database
,並以此為seed
。
Update-Database
觸發“ 更新 ”操作。 您可以運行您的應用程序,並且數據庫將在應用程序啟動時更新。
我更喜歡手動運行Update-Database
,因為我可以查看是否有任何數據庫遷移錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.