I have 2 object - parents and childs in relationship many-to-many, the issue is that 1 parent can have 2 the same childs, but EF saves this relationship only 1 time.
I found only 2 working solutions:
- add
count
column into table and manually fill it - not use many-to-many, but split it into one-to-many and many-to-one but I do not like this solutions, because I hope that there could be some more simple solution.
Could you help me please?
EDIT: Example of junction table:
1-1
1-1
1-2
1-3
2-3
Code: model
public class Item
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public virtual ICollection<Item> childs { get; set; }
public virtual ICollection<Item> parents { get; set; }
}
DB context
modelBuilder.Entity<Item>().
HasMany(i => i.childs).
WithMany(i2 => i2.parents).
Map(
m =>
{
m.MapLeftKey("parentId");
m.MapRightKey("childId");
m.ToTable("itemRelationship");
});