Maintain denormalized data with NHibernate EventListener
Posted
by Michael Valenty
on Stack Overflow
See other posts from Stack Overflow
or by Michael Valenty
Published on 2009-10-30T05:32:34Z
Indexed on
2010/03/22
1:41 UTC
Read the original article
Hit count: 418
nhibernate
I have one bit of denormalized data used for performance reasons and I'm trying to maintain the data with an NHibernate event listener rather than a trigger. I'm not convinced this is the best approach, but I'm neck deep into it and I want to figure this out before moving on. I'm getting following error:
System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
System.Collections.Generic.List`1.Enumerator.MoveNextRare()
System.Collections.Generic.List`1.Enumerator.MoveNext()
NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
NHibernate.Engine.ActionQueue.ExecuteActions()
NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions (IEventSource session)
NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
NHibernate.Impl.SessionImpl.Flush()
NHibernate.Transaction.AdoTransaction.Commit()
Here's the code to make happen:
using (var tx = session.BeginTransaction())
{
var business = session
.Get<Business>(1234)
.ChangeZipCodeTo("92011");
session.Update(business);
tx.Commit(); // error happens here
}
and the event listener:
public void OnPostUpdate(PostUpdateEvent @event)
{
var business = @event.Entity as Business;
if (business != null)
{
var links = @event.Session
.CreateQuery("select l from BusinessCategoryLink as l where l.Business.BusinessId = :businessId")
.SetParameter("businessId", business.BusinessId)
.List<BusinessCategoryLink>();
foreach (var link in links)
{
link.Location = business.Location;
@event.Session.Update(link);
}
}
}
© Stack Overflow or respective owner