Entity framework EF 6代码优先迁移警告或防止破坏性更新
问题:在运行迁移脚本时,是否存在警告数据意外丢失的设置 从4.1开始,我一直在使用代码优先迁移,但在一个测试项目中进行实验时,我遇到了意想不到的行为。也许我没有跟上更新的功能 假设我有一个简单的模型:Entity framework EF 6代码优先迁移警告或防止破坏性更新,entity-framework,asp.net-mvc-5,entity-framework-6,entity-framework-migrations,Entity Framework,Asp.net Mvc 5,Entity Framework 6,Entity Framework Migrations,问题:在运行迁移脚本时,是否存在警告数据意外丢失的设置 从4.1开始,我一直在使用代码优先迁移,但在一个测试项目中进行实验时,我遇到了意想不到的行为。也许我没有跟上更新的功能 假设我有一个简单的模型: public class Customer { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } public s
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
和背景:
public class CustContext : DbContext
{
public CustContext() : base("DefaultConnection") { }
public DbSet<Customer> Customers { get; set; }
}
然后我把地址改成了家庭地址。这是迁移脚本:
add-migration HomeAddress
public partial class HomeAddress : DbMigration
{
public override void Up()
{
AddColumn("dbo.Customers", "HomeAddress", c => c.String());
DropColumn("dbo.Customers", "Address");
}
public override void Down()
{
AddColumn("dbo.Customers", "Address", c => c.String());
DropColumn("dbo.Customers", "HomeAddress");
}
}
internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstMigrations.Model1.CustContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
}
protected override void Seed(CodeFirstMigrations.Model1.CustContext context)
{
}
}
这是配置脚本:
add-migration HomeAddress
public partial class HomeAddress : DbMigration
{
public override void Up()
{
AddColumn("dbo.Customers", "HomeAddress", c => c.String());
DropColumn("dbo.Customers", "Address");
}
public override void Down()
{
AddColumn("dbo.Customers", "Address", c => c.String());
DropColumn("dbo.Customers", "HomeAddress");
}
}
internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstMigrations.Model1.CustContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;
}
protected override void Seed(CodeFirstMigrations.Model1.CustContext context)
{
}
}
内部密封类配置:dbmigtorinsconfiguration
{
公共配置()
{
AutomaticMiggerationsEnabled=假;
AutomaticMigrationDataLossAllowed=false;
}
受保护的覆盖无效种子(CodeFirstMigrations.Model1.CustContext上下文)
{
}
}
当我运行updatedatabase命令时,我完全预料到会出现这样一个错误:“无法完成迁移,因为这将导致数据丢失…”实际上,我在自己的项目中经常看到这个错误
但是相反,它愉快地删除了(填充的)地址列,并创建了一个新的列HomeAddress
我认为必须有一个配置设置来控制这种行为,但我所能找到的只是
AutomaticMigrationDataLossAllowed
,它显然只适用于自动迁移
我忽略了什么吗 这已经作为一个问题提出,但被拒绝为“设计” 决定这是出于设计-我们只对数据丢失的自动迁移发出警告 查看错误。您可以使用:
public partial class HomeAddress : DbMigration
{
public override void Up()
{
AddColumn("dbo.Customers", "HomeAddress", c => c.String());
Sql("UPDATE "dbo.Customers" SET HomeAddress = Address");
DropColumn("dbo.Customers", "Address");
}
public override void Down()
{
AddColumn("dbo.Customers", "Address", c => c.String());
DropColumn("dbo.Customers", "HomeAddress");
}
}
然后保存数据。AutomaticMigrationDataLossSallowe正在做它的意思,我想我有点困惑。AutomaticMigrationDataLossSallowe=False在我描述的场景中没有任何更改,因为这些不是自动迁移您的迁移设置是什么?抱歉,J.W。,我不确定你指的是什么“迁移设置”?我确实用配置类和更多迁移步骤细节更新了我的问题。如果我使用自动迁移(我从来没有这样做过),我会收到关于数据丢失的警告。如果我使用显式迁移,我不会收到关于数据丢失的警告。我认为过去确实如此,但也许我错了。但这是我的目标,提醒我显式迁移会导致数据丢失。这很不令人满意,但我认为这必须算作一个正确的答案。:)