考虑一个定义为:
的表 public class SkillInteraction
{
// Primary Key
[Key]
public int SkillInteractionId { get; set; }
// record type (offensive, defensive)
public int SkillTypeId { get; set; }
//foreign key to Skill table
public int ParentSkillId { get; set; }
//foreign key to Skill table
[ForeignKey("ParentSkillId")]
public virtual Skill ParentSkill { get; set; }
//foreign key to Skill table
public int SkillId { get; set; }
// list of skills associated with the parent skill
[ForeignKey("SkillId")]
public virtual Skill Skill { get; set; }
public decimal Ratio { get; set; }
}
public class Skill
{
public int SkillId { get; set; }
// record type (offensive, defensive)
public int SkillTypeId { get; set; }
public string SkillName { get; set; }
}
我相当肯定我需要使用流利的语法来映射这种关系,只是不确定如何去做。如果我按原样保留它,我将使用外键导致循环错误。
Introducing FOREIGN KEY constraint 'SkillInteraction_Skill' on table 'SkillInteractions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.
那么,我如何在EF 4.2中设置这种关系,以便SQL服务器不抱怨?
答案 0 :(得分:1)
你需要通过流畅的api指定关系,默认情况下,ef会打开WillCascadeOnDelete
选项,你需要将其关闭。它不能通过注释来完成。
我在我的博客http://2guysfrommumbai.wordpress.com/2011/10/26/entity-framework-4-1-associations/上写过这篇文章。希望这会有所帮助。