假设我有这门课程:
public class Element
{
[Key]
public int ElementId { get; set; }
public int? ParentId { get; set; }
[ForeignKey("ParentId")]
public virtual Element Parent { get; set; }
[Required, MaxLength(100)]
public string Name { get; set; }
public Element(string name, Element parent)
{
Name = name;
Parent = parent;
}
}
现在我有了
Element A = new Element("A", null);
Element B = new Element("B", A);
Element C = new Element("C", A);
Context.Elements.Add(A);
Context.Elements.Add(B);
Context.Elements.Add(C);
EF立即在DB中写入三份A副本。除非它不存在,我如何告诉EF不要写父元素?