[英]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.