fluent nhibernate: Mapping a Many-to-many realationship as a one-to-many relationship
We a mant-to-many relationship modeled in开发者_Go百科 the database (with a bridge table) between Student and Professor, but in our entites we want to model it as a one-to-many relationship i.e. a Student has one Professor.
Here is our attempt, but it doesnt work:
protected StudentMap()
{
Id(x => x.Id);
Map(x => x.Name);
Join("student_professor_selected", m =>
{
m.KeyColumn("student_professor_selected_key");
m.References(x => x.Professor);
});
}
Join is something completely different. It is used to put parts of a class into another table (one-to-one).
You just map a many-to-many relation from professor to student, it is always a simple List in C#.
It's not possible to map it as a single ending reference from student to professor. But you could map a list of professors in a private property and implement a Professor property which just returns the first element:
private IList<Professor> professors;
public Professor Professor { get { return professors.First(); } }
Thanks for the help, my solution was as follows. Within the professors class I added the following:
private IList<Professor> _professors;
public Professor Professor { get { return _professors.First(); } }
and then to map this i did the following, where professor_student_selected is the bridge table to map the professor keys with student keys
HasManyToMany<Students>(Reveal.Member<Professor>("_professors")).Table("professor_student_selected").ChildKeyColumn("student_key").ParentKeyColumn("professor_key");
精彩评论