문제

Can someone please post an example on how to create a many to many relationship where the source class is also the target one, using code-first ?

Something like:

Toys * <-----> * Toys

Thank you.

Nuno Senica

도움이 되었습니까?

해결책

I don't think this is possible with EF Code First. As a workaround, you could create the mapping table yourself:

public Toy
{
    public int ToyID {get; set;}
    public ICollection<ToyMapping> Toys {get; set;}
}

public ToyMapping
{
    public int ToyOneID {get; set;}
    public int ToyTwoID {get; set;}

    public ICollection<Toy> ToyOnes {get; set;}
    public ICollection<Toy> ToyTwos {get; set;}
}

I'm not sure the actual use-case for this is, otherwise I would have made better named properties.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top