添加迁移后尝试更新数据库时出现错误。
这是我在添加迁移之前的课程
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
命令,这是我得到的:
ALTER TABLE 语句与 FOREIGN KEY 约束“FK_dbo.ProductFeatures_dbo.Products_ProductId”冲突。冲突发生在数据库“CBL”、表“dbo.Products”、列“ProductId”中
我可以做些什么来解决这个问题并使我的迁移恢复正常?