- edited description
- marked as minor
Many-to-many with additional properties in the intermediate entity possible?
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)
-
reporter -
reporter - edited description
-
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? -
reporter Thanks, Martin.
-
You can still work as usually with a
ManyToMany
relationship, and you can use aOneToMany
relationship toStudentSubject
when you need to access or modify the intermediate table. You can set theManyToMany
relationship toReadOnly
to make sure that the intermediate data is not overriden when updating your object. -
reporter Thank you, Guillermo.
-
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.
- Log in to comment