【发布时间】:2012-11-30 17:36:54
【问题描述】:
添加迁移后尝试更新数据库时出现错误。
这是我在添加迁移之前的课程
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”
我可以做些什么来解决这个问题并让我的迁移恢复正常?
【问题讨论】:
-
Feature中的ProductId值不会出现在Product中。
标签: entity-framework foreign-keys entity-framework-migrations