一对多关系-级联删除
|
我正在使用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; }
}
没有找到相关结果
已邀请:
1 个回复
容淑阔九