I have a bi-directional, many-to-many association between EntityA and EntityB and I’m using an association class, EntityABLink, to model this because there are other attributes about the relationship that I need to track. In addition, I also have another class that holds a reference to a specific relationship between EntityA and EntityB so I treat the association class as a full-fledged entity.
In EntityA I have a read-only property that returns a list of associated EntityB objects and, likewise, in EntityB I have a read-only property that returns a list of associated EntityA objects. Note that, for these properties, I’m hiding the fact that the association is implemented via an association class. (I also have dedicated methods for updating the relationships that hide the implementation.) Behind the scenes in both EntityA and EntityB, I have private collections of type EntityABLink.
Since a picture is worth a thousand words, here is what I have described so far:
(Note again that the public, read-only properties on EntityA and EntityB are not of the same type as the private members that back them up.)
So far, so good. Now I want to persist these objects to a database using Fluent NHibernate automapping overrides. When it comes to mapping, I like to think of the above using this functionally equivalent representation:
From this diagram, it’s clear that what I really need is two bi-directional one-to-many relationships.
In mapping the above, I figure that I need something like this:
In the EntityA automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityA>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate();
mapping.IgnoreProperty(x => x.EntityBList);
In the EntityB automapping override:
mapping.HasMany<EntityABLink>(Reveal.Member<EntityB>(“_AssociationList”)).Inverse().AsBag().Cascade.SaveUpdate();
mapping.IgnoreProperty(x => x.EntityAList);
In the EntityABLink automapping override:
mapping.References<EntityA>(x => x.EntityA).Not.Nullable();
mapping.References<EntityB>(x => x.EntityB).Not.Nullable();
When I try this, however, I get the following error:
"Could not find a getter for property '_ AssociationList’ in class 'EntityB'."
I must have something wrong with my mappings, but I’m not sure what. Any ideas?