親からデータ コレクションを取得するには、ID が必要です。計画は、ParentId を使用してリポジトリからすべてのフレンドをフェッチすることです。
説明するために、私は親と子のクラスとそれぞれ 1 つのマッパーを持っています。ParentMapper でキーを定義する必要があると思います。
私のコードのコメントを参照してください
{
public class Child {
public int IdChild { get; set; }
public string ChildName {get;set;}
// here is the problem
//I need to define ParentID as ForeignKey in some way but how??
//I belive its done in the ParentMapper
public virtual int ParentId { get; set; } //ForeignKey
public virtual ICollection<Friend> Friends { get; set; }
//The plan is to fetch all friends from the repository by using the ParentId and
//keep the entity as clean as possible.
public virtual ICollection<Friend> FamilyFriends { get; set; }
}
public class Parent {
public int IdParent { get; set; }
public string ParentName {get;set;}
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<Child> Children { get; set; }
}
}
{
class ParentMapper : EntityTypeConfiguration<Parent>
{
public ParentMapper()
{
HasKey(one => one.IdParent);
//I started out like this but its not possible....
//But this will give an error obviusly
HasMany(c => c.Children).WithRequired(d => d.ParentId).HasForeignKey(one => one.ParentId);
}
}
}
{
class ChildMapper : EntityTypeConfiguration<Child>
{
public ChildMapper()
{
HasKey(one => one.IdChild);
}
}
}