1

We a mant-to-many relationship modeled in 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);
             });
        }
super ted
  • 21
  • 2

2 Answers2

0

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(); } }
Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
  • So how would we map this? you cant do this ... ` HasManyToMany(x=>x.Professors)` be cause the field is private, and we cant use the public property as that is not a list. – super ted Sep 09 '11 at 13:27
  • Sorry, I can't say how the mapping exactly looks like, I'm not using Fluent ... It must be some kind of HasManyToMany. There must be some way to map fields. – Stefan Steinegger Sep 09 '11 at 14:35
  • Take a look at this post on how to map private collections in FNH http://stackoverflow.com/questions/781443/private-collection-mapping-in-fluent-nhibernate – Cole W Sep 09 '11 at 19:47
0

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");
super ted
  • 21
  • 2