我在理解實體框架代碼優先關係創建時遇到問題,因爲我更習慣於傳統方式。實體框架 - 關係混淆
一對多的關係對我來說似乎很清楚:子女只需要一個foreignKey ID屬性來指示他們屬於哪個父母。
public class Parent
{
public int Id { get; set; }
public virtual ICollection<Child> Childs { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
現在,我不太清楚如何正確地創建一個許多一對多關係。可能需要額外的表ParentChild
,因此不需要(外鍵)ID屬性?現在
public class Parent
{
public int Id { get; set; }
public virtual ICollection<Child> Childs { get; set; }
}
public class Child
{
public int Id { get; set; }
public virtual ICollection<Parent> Parents { get; set; }
}
,爲一到一個關係,我不知道。 public class Parent { public int Id {get;組; }
public int ChildID { get; set; }
public virtual Child child { get; set; }
}
public class Child
{
public int Id { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
}
是國外ID性能,即使需要或者我可以只是在Parent
類Child
屬性和Parent
型物業在Child
類?當我省略外鍵ID屬性時,允許使用virtual
關鍵字嗎?