Many-to-many with additional properties in the intermediate entity possible?

Issue #106 new
Uwe Keim created an issue

In your example on the front page, I want to add an additional property in the intermediate entity like e.g.:

public class Student
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Name { get; set; }

    [ManyToMany(typeof(StudentSubject))]
    public List<Subject> Subjects { get; set; } 
}

public class Subject
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    public string Description { get; set; }

    [ManyToMany(typeof(StudentSubject))]
    public List<Student> Students { get; set; } 
}

public class StudentSubject
{
    [ForeignKey(typeof(Student))]
    public int StudentId { get; set; }

    // HERE, THIS IS NEW:
    public int OrderPosition { get; set; }

    [ForeignKey(typeof(Subject))]
    public int SubjectId { get; set; }
}

And I also want to access (get/set) the new property (OrderPosition) by code.

My question:

Is something like this possible with the ManyToMany attribute or should (can?) I manually model it by using OneToMany instead?

Comments (7)

  1. Martin Kuckert

    Not sure if this is possible - would say now. For me it looks like you have to use OneToMany relations and do the selection on your own. @redent Can you help?

  2. Guillermo GutiƩrrez

    You can still work as usually with a ManyToMany relationship, and you can use a OneToMany relationship to StudentSubject when you need to access or modify the intermediate table. You can set the ManyToMany relationship to ReadOnly to make sure that the intermediate data is not overriden when updating your object.

  3. Erwin Warrinnier

    I do not understand the explanation of Guillermo completely. Can anyone share sourcecode sample to make it clear, since I share this same question on adding more fields to the intermediate class.

  4. Log in to comment