我有一些非规范化数据用于性能原因,我试图用NHibernate事件监听器而不是触发器来维护数据。我不相信这是最好的方法,但我深入研究它,我想在继续前进之前想出来。我收到了以下错误:
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()
以下是要实现的代码:
using (var tx = session.BeginTransaction())
{
var business = session
.Get<Business>(1234)
.ChangeZipCodeTo("92011");
session.Update(business);
tx.Commit(); // error happens here
}
和事件监听器:
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);
}
}
}
答案 0 :(得分:1)
这看起来与NHibernate无关,而是C#处理迭代器的方式,特别是Enumerations。我有点猜测,但我认为这是因为你在这一行修改了枚举的值:link.Location = business.Location;
。快速谷歌搜索告诉我Enumerator.Current
属性是只读的(这是使用foreach
构造时使用的属性)。我打赌使用常规的for
循环可以解决这个问题。