EF 6 Code First Many to many With Payload and self referencing many to many
Posted
by
lesley86
on Stack Overflow
See other posts from Stack Overflow
or by lesley86
Published on 2014-06-09T23:10:11Z
Indexed on
2014/06/10
3:25 UTC
Read the original article
Hit count: 286
entity-framework
|ef-code-first
|many-to-many
|code-first-migrations
|self-referencing-table
I Have the problem where i have a many to many relationship and on one of the tables there will be a self referencing many to many.
So basically a school have zero or many groups and many groups can have 0 or many schools. The groups table will contain a parent child many to many with itself because a group can be a child of another group or it can have no children and that child can have a child, one child can also have many parents or a entity can have no parents.
I created a mapping table with Payload to solvethe first many to many problem. code snippet
public class School
{
public virtual ICollection<SchoolGroupMap> SchoolGroupMaps
}
public class SchoolGroup
{
public virtual ICollection<SchoolGroupMap> SchoolGroupMaps
}
public class SchoolGroupMap
{
public virtual School School
public virtual SchoolGroup SchoolGroup
}
i Then tried modifying the code the following way for the the self referencing many to many
public class SchoolGroup
{
public virtual ICollection<SchoolGroupMap> SchoolGroupMaps
public virtual ICollection<SchoolGroup> Parents
public virtual ICollection<SchoolGroup> Children
}
I changed the context with has many and an auto mapping table (forgive me i have been trying so many things today i do not have the exact code). I received an error the properties on the classes must match.
Can anyone help please.
I want to do create navigation properties on the self referencing many to many. Also a seed example would be appreciated
regards
© Stack Overflow or respective owner