Entity Framework 4 CTP 5 Self Referencing Many-to-Many

前端 未结 1 977
梦如初夏
梦如初夏 2020-12-15 11:41

I have the following scenario in my database. It is a record of Studies and those studies have other studies as prerequisites. In my DB design, it looks like this:

<
相关标签:
1条回答
  • 2020-12-15 12:25

    This is what I have in an EntityTypeConfiguration<> implementation for a similar situation in CTP5.

    HasMany(g => g.SubGroups)
        .WithMany(g => g.ParentGroups)
        .Map(m => m.ToTable("Groups_SubGroups"));
    

    Not sure exactly how that translates to configuring the DbContext directly, but I imagine it should be close.

    If memory serves, LeftKey() RightKey() syntax wasn't quite there in CTP5, so you just have to use the default column names that it creates or is expecting. In my case, it is GroupId and GroupId1. That follows the pattern <class>Id and <class>Id1, not <field> and <field>1 by the way.

    The error that you're getting does seem familiar and I don't remember that the solution was obvious in any way. But, I did set this all up a while ago so the memories of how I arrived at something that works is a bit swiss cheesed. Hope it helps some.

    0 讨论(0)
提交回复
热议问题