Say, for example, you have the following two classes.
1. Definition 2. Children
For many, many relationship relationships, you have to put the class of the parent class ICollection in the child class and put the ICollection of the child class in the parent class, as shown below.
public class Definition {
another class (children) must have the ICollection of Definition class.
public class Children {
In the DataContext, you must create a mapping for the new table, as shown below,
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Definition>() .HasMany(l => l.Childrens) .WithMany(o => o.Definitions) .Map(m => { m.MapLeftKey("DefinitionId"); m.MapRightKey("ChildrenId"); m.ToTable("ChildrenDefinitions"); }); }
Note. You will have a separate table created using two columns (DefinitionId, ChildrenId). And this table (ChildrenDefinitions) will allow you to create a many-to-many relationship with the definition class and the children class.
Hope this helps you!
Vignesh
source share