首先使用代码,我有一些抽象类和一些从这些抽象类派生的类。
// Abstracted Classes
public abstract class Brand
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
public abstract class Model
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
// Derived Classes
[Table("ComparisonBrand")]
public class ComparisonBrand : Brand
{
public ComparisonBrand()
{
ComparisonValues = new List<ComparisonValue>();
Models = new List<ComparisonModel>();
}
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
public virtual ICollection<ComparisonModel> Models { get; set; }
}
[Table("ComparisonModel")]
public class ComparisonModel : Model
{
public int? BrandId { get; set; }
public int? LogoId { get; set; }
[ForeignKey("BrandId")]
public virtual ComparisonBrand ComparisonBrand { get; set; }
[ForeignKey("LogoId")]
public virtual ComparisonLogo ComparisonBrand { get; set; }
public virtual ICollection<ComparisonValue> ComparisonValues { get; set; }
}
我的问题是迁移会为以下内容生成外键:
- 比较模型.Id > 模型.Id
- ComparisonModel.BrandId > Brands.Id
- 比较模型.品牌 ID > 比较品牌. ID
由于 ComparisonBrand.Id 是 Brands.BrandId 的 FK,因此在删除品牌记录时出现错误。但是,如果我删除了 ComparisonModel.BrandId > ComparisonBrand.Id 关系,则删除工作正常。
如何防止在抽象表和派生表(品牌和比较品牌)之间形成关系?