ManyToMany should allow doublets.

Issue #96 new
Tobias Malm created an issue

E.g. if i have:

public class Car
{
    public Car() {}

    [PrimaryKey]
    public string Name { get; set; }
    public string Data{ get; set; }
    [ManyToMany(typeof(Cars_Wheels))]
    public List<Wheel> Wheels{ get; set; }
}

public class Wheel
{
    public Wheel() {}

    [PrimaryKey]
    public string Name { get; set; }
    public int Diameter{ get; set; }
    [ManyToMany(typeof(Cars_Wheels))]
    public List<Car> Cars{ get; set; }
}

public class Cars_Wheels
{
    [ForeignKey(typeof(Car))]
    public string CarName{ get; set; }

    [ForeignKey(typeof(Wheel))]
    public string WheelName{ get; set; }
}

I want to have several wheels (of the same type) on one (or several cars) I can't at the moment. As of this line (578) of code in the function UpdateManyToManyForeignKeys in the file WriteOperatons.cs:

var missingChildKeyList = childKeyList.Where(o => !currentChildKeyList.Contains(o)).ToList();

Removes all with the same PK. An ugly solution is to replace that line of code with the following:

foreach (var child in currentChildKeyList)
{
    missingChildKeyList.Remove(child);
}

There are probably smarter ways of doing it ...

Comments (4)

  1. Guillermo GutiƩrrez

    So you want duplicated entries in a ManyToMany relationship, right? I think that the default behavior should stay like it's now (i.e. like it was a Set), that it's the default behavior on most frameworks, but we could add a property to ManyToMany attribute to support it. Maybe something like this:

    [ManyToMany(typeof(Cars_Wheels), AllowDuplicates = true)]
    public List<Wheel> Wheels{ get; set; }
    

    What do you think?

  2. Martin Kuckert

    I'm a bit irritated about this design, but find no good reason to prevent multiple references between the same instances.

    But I'm with @redent : If the library supports this, it should be Opt-In because most people would probably not expect this behavior.

  3. Log in to comment