一对多关系-级联删除

| 我正在使用EF 4.1,试图将POCO映射到现有数据库。在我尝试删除另一个项目所依赖的项目之前,此方法工作正常。我要启用级联删除,以便在删除第一项时,所有依赖项也将被删除(我相信这称为级联删除)。 我试图在OnModelCreating中启用它:
modelBuilder.Entity<Component>()
         .HasMany(c => c.Specifications)
         .WithRequired(s => s.Component)
         .Map(m => m.MapKey(\"ComponentId\"))
         .WillCascadeOnDelete(true);
但是,我仍然遇到“ 1”例外。 该数据库非常简单: 零件: ComponentId(PK) 描述 规格: 规格ID(PK) 描述 ComponentID(FK) 我创建了以下两个类以匹配此设置:
public class Specification
{
    [Key]
    [Required]
    public int Id { get; set; }

    [MaxLength(50)]
    [Required]
    public string Description { get; set; }

    public virtual Component Component { get; set; }
}
public class Component
{
    [Key]
    [Required]
    public int Id { get; set; }

    [MaxLength(50)]
    [Required]
    public string Description { get; set; }

    public virtual ICollection<Specification> Specifications { get; set; }
}
    
已邀请:
        模型中的级联删除需要在数据库中级联删除。如果让EF为您重新创建数据库,它将自动进行设置。如果您不能让EF这样做,则您必须: 手动将级联删除添加到FK,或者 从模型中删除级联。     

要回复问题请先登录注册