我试图使用相同的类型获得一对多的关系。
e.g。
public class CompanionLinkedCompanion
{
public int CompanionId { get; set; }
[ForeignKey("CompanionId")]
public virtual Companion Companion { get; set; }
public int LinkedCompanionId { get; set; }
[ForeignKey("LinkedCompanionId")]
public virtual Companion LinkedCompanion { get; set; }
}
然后
public class Companion
{
public virtual ICollection<CompanionLinkedCompanion> CompanionLinkedCompanions { get; set; }
}
当我尝试此功能时,链接随播广告的随播广告系列不会被填充。
感谢任何帮助。
使用@iraj part-solution修复更新:
public class CompanionLinkedCompanion : Entity
{
public int Id { get; set; }
public int CompanionId { get; set; }
[ForeignKey("CompanionId")]
[InverseProperty("CompanionLinkedCompanions")]
public virtual Companion Companion { get; set; }
public int LinkedCompanionId { get; set; }
[ForeignKey("LinkedCompanionId")]
public virtual Companion LinkedCompanion { get; set; }
}
答案 0 :(得分:0)
使用它:
public class CompanionLinkedCompanion
{
public int CompanionId { get; set; }
[ForeignKey("CompanionId")]
[InverseProperty("CompanionLinkedCompanions")]
public virtual Companion Companion { get; set; }
public int LinkedCompanionId { get; set; }
[ForeignKey("LinkedCompanionId")]
[InverseProperty("LinkedCompanions")]
public virtual Companion LinkedCompanion { get; set; }
}
public class Companion
{
[InverseProperty("Companion")]
public virtual ICollection<CompanionLinkedCompanion> CompanionLinkedCompanions { get; set; }
[InverseProperty("LinkedCompanion")]
public virtual ICollection<CompanionLinkedCompanion> LinkedCompanions { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CompanionLinkedCompanion>().HasRequired(x => x.Companion).WithMany().HasForeignKey(x => x.CompanionId).willCascadeOnDelete(false);
modelBuilder.Entity<CompanionLinkedCompanion>().HasRequired(x => x.LinkedCompanion).WithMany().HasForeignKey(x => x.LinkedCompanionId);
}
答案 1 :(得分:0)
对于一对多关系,您需要标准的父/子关系(只需要一个实体类):
public class Companion
{
public int CompanionId { get; set; }
public int? LinkedCompanionId { get; set; }
[ForeignKey("LinkedCompanionId")]
// ParentCompanion
public virtual Companion LinkedCompanion { get; set; }
[InverseProperty("LinkedCompanion")]
// ChildrenCompanions
public virtual ICollection<Companion> CompanionLinkedCompanions { get; set; }
}