views:

144

answers:

1

I have two objects that have a ManyToMany relationship with one another through a mapping table. Though, when I try to save it, I get a stack overflow exception. The following is the code for the mappings:

//EventMapping.cs
HasManyToMany(x => x.Performers).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("EventId").ChildKeyColumn("PerformerId");


//PerformerMapping.cs
HasManyToMany<Event>(x => x.Events).Table("EventPerformer").Inverse().Cascade.AllDeleteOrphan().LazyLoad().ParentKeyColumn("PerformerId").ChildKeyColumn("EventId");

When I change the performermapping.cs to Cascade.None() I get rid of the exception but then my Event Object doesn't have the performer I associate with it.

//In a unit test, paraphrased
event.Performers.Add(performer); //Event
eventRepository.Save<Event>(event);
eventResult = eventRepository.GetById<Event>(event.id); //Event
eventResult.Performers[0]; //is null, should have performer in it

How should I be writing this properly? Thanks

+2  A: 

You are declaring both sides of the relationship as Inverse, which means no one is responsible for saving the relationship, and of course will not work.

Since you are adding events to performers, remove the Inverse call from PerformerMapping.

Also, unless you Flush the session and then Evict the event, a call to session.Get using the same session will return the same object.

Diego Mijelshon
Also wrap saving of entities in transaction.
Sly