您可以創建具有兩個實體鍵的關聯模型:
public class AssociativeEntity
{
[Key]
public Guid AssociativeEntityId { get; set; }
public Guid Entity1Id { get; set; }
public Guid Entity2Id { get; set; }
[Display(Name = "Entity1", ResourceType = typeof(Resources.Language))]
public virtual Entity1 Entity1 { get; set; }
[Display(Name = "Entity2", ResourceType = typeof(Resources.Language))]
public virtual Entity2 Entity2 { get; set; }
}
實體1:
public class Entity1
{
[Key]
public Guid Entity1Id { get; set; }
/* Describe the other properties here */
[Display(Name = "AssociativeEntities", ResourceType = typeof(Resources.Language))]
public virtual ICollection<AssociativeEntity> AssociativeEntities { get; set; }
}
實體2:
public class Entity2
{
[Key]
public Guid Entity2Id { get; set; }
/* Describe the other properties here */
[Display(Name = "AssociativeEntities", ResourceType = typeof(Resources.Language))]
public virtual ICollection<AssociativeEntity> AssociativeEntities { get; set; }
}
謝謝您的回答。通過「關聯模型」,你是指我將爲EF創建的中間表或連接表指定模型? – lbrahim
是的。 modelBuilder的問題是您必須使用關聯模型的有限選項集。出於這個原因,我建議你手動創建模型。 –
好的,謝謝。我會給它一個鏡頭。 – lbrahim