添加迁移后尝试更新我的数据库时出现错误。
这是我在添加迁移之前的类(class)
public class Product
{
public Product() { }
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public bool Istaxable { get; set; }
public string DefaultImage { get; set; }
public IList<Feature> Features { get; set; }
public IList<Descriptor> Descriptors { get; set; }
public IList<Image> Images { get; set; }
public IList<Category> Categories { get; set; }
}
public class Feature
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
现在我想在我的 Feature 类中添加一个外键并以这种方式重构类:
public class Product
{
public Product() { }
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public bool Istaxable { get; set; }
public string DefaultImage { get; set; }
public IList<Feature> Features { get; set; }
public IList<Descriptor> Descriptors { get; set; }
public IList<Image> Images { get; set; }
public IList<Category> Categories { get; set; }
}
public class Feature
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public string VideoLink { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
}
我添加了一个迁移
Add-Migration
命令。我添加了一个
Update-Database
命令,这是我得到的:The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.ProductFeatures_dbo.Products_ProductId". The conflict occurred in database "CBL", table "dbo.Products", column 'ProductId'
我该怎么做才能解决这个问题并使我的迁移恢复正常?
最佳答案
解决这个问题的关键是把你的迁移分成两次迁移。首先,添加一个可为空的字段并填写数据。其次,使该字段成为必需的外键。
第一次迁移
public class MyOtherEntity
{
public int Id { get; set; }
}
public class MyEntity
{
...
// New reference to MyOtherEntity
public int? MyOtherEntityId { get; set; }
...
}
> add-migration AddMyEntityMyOtherEntity1
public partial class AddMyTableNewProperty1 : DbMigration
{
public override void Up()
{
AddColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
}
public override void Down()
{
DropColumn("dbo.MyEntity", "MyOtherEntityId");
}
}
public partial class AddMyTableNewProperty1 : DbMigration
{
public override void Up()
{
AddColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
// ADD THIS BY HAND
Sql(@"UPDATE dbo.MyEntity SET MyOtherEntityId = 1
where MyOtherEntity IS NULL");
}
public override void Down()
{
DropColumn("dbo.MyEntity", "MyOtherEntityId");
}
}
> update-database
第二次迁移
public class MyEntity
{
...
// Change the int? to int to make it mandatory
public int MyOtherEntityId { get; set; }
// Create a reference to the other entity
public virtual MyOtherEntity MyOtherEntity { get; set; }
...
}
> add-migration AddMyEntityMyOtherEntity2
public partial class AddMyEntityMyOtherEntity2: DbMigration
{
public override void Up()
{
AlterColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int(nullable: false));
CreateIndex("dbo.MyEntity", "MyOtherEntityId");
AddForeignKey("dbo.MyEntity", "MyOtherEntityId", "dbo.MyOtherEntity", "Id");
}
public override void Down()
{
DropForeignKey("dbo.MyEntity", "MyOtherEntityId", "dbo.MyOtherEntity");
DropIndex("dbo.MyEntity", new[] { "MyOtherEntityId" });
AlterColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
}
}
> update-database
其他注意事项
关于entity-framework - 使用 Code First 迁移添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13650257/