我只是到达了一个我开始了解更新过程中EF6的操作的程度,并且只是弄清楚了自动迁移Enabled的实际功能。我正在尝试使用新用户表更新数据库。我为用户创建了一个新实体:
[Table("Users")]
public class User
{
<Omitted properties>
}
在我一直从事的项目中,已选择使用明确的迁移而没有任何自动迁移。因此,我创建了一个迁移脚本来创建数据库:
public partial class AddingUserTable : DbMigration
{
public override void Up()
{
CreateTable("dbo.Users",.... Omitted for clarity
}
public override void Down()
{
DropTable("dbo.Users");
}
}
并更新我的上下文,因此我可以使用以下方式访问它:
public DbSet<User> Users { get; set; }
此时,如果我在Nuget软件包管理器上执行" Update-Database",它将应用迁移脚本,但带有警告:
Applying explicit migration: 201712201003395_AddingUserTable.
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
事实证明,这是因为我将DBSET用户添加到上下文中。如果我删除该DBSET并再次更新数据库,则不会显示警告。如果添加,警告将再次出现。我知道警告之所以出现,是因为我通过添加集合而更改了上下文,并且我已经禁用了自动迁移,但是我已经通过显式迁移脚本应用了更改。
我需要做什么,因此实体框架可以看到我已经为此进行了迁移并接受新的DBSET用户没有警告?
看起来您的实体中还没有迁移中未考虑的其他更改。做" Add-Migration
"应该在您面前带来这些更改。