开发者

Code-First: How to create a many to many relationship where the source class/table is the target one as well?

开发者 https://www.devze.com 2023-01-29 02:46 出处:网络
Can someone please post an example on how to create a many to many relations开发者_运维知识库hip where the source class is also the target one, using code-first ?

Can someone please post an example on how to create a many to many relations开发者_运维知识库hip 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消