Having found and followed the solution in this thread
http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/cebc70ff873e4fd2/ce6cc622bc02d9c
I still come up empty handed while trying to solve the following
problem:
I have a bi-directional one-to-many relationship between two entities,
Match and Goal. I have the following domain objects:
public class Match
{
public virtual int Id { get; private set; }
public virtual IList<Goal> Goals { get; set; }
public virtual void AddGoal(Goal goal)
{
goal.Match = this;
Goals.Add(goal);
}
}
public class Goal
{
public virtual int Id { get; private set; }
public virtual Match Match { get; set; }
}
The Goal can have only one Match, and a Match can have multiple Goals.
This is the mappings:
public MatchMap()
{
Id(x => x.Id);
HasMany(x => x.Goals)
.Inverse()
.Cascade.All()
.AsSet();
}
public GoalMap()
{
Id(x => x.Id);
References(x => x.Match).Not.Nullable();
}
When I perform this test I get a “not-null property references a null
or transient value Goal.Match”
var goals = new List<Goal> { new Goal() };
new PersistenceSpecification<Match>(Session, new
CustomEqualityComparer())
.CheckProperty(c => c.Id, 1)
.CheckList(c => c.Goals, goals, (match, goal) =>
match.AddGoal(goal))
.VerifyTheMappings();
At first I didn’t have the AsSet() on my MatchMap, but having read
this:
http://www.nhforge.org/doc/nh/en/index.html#collections-bidirectional,
it seems that it is required to use either bag or set and not list.
If I remove the Not.Nullable on the reference to Match in GoalMap the
test works, but I want to have that constraint, since it doesn’t make
sense to have a Goal without it being tied to a Match.
Is there anybody out there, who can help me solve this problem (which
I’m sure is pretty trivial once I reach enlightment ….)