NHibernate collection loading, "illegal access to loading collection"

Viewed 8141

I have a problem loading a collection. Setup I have is simple one-to-many association, mapped using FluentNHibernate. Entity is loaded withough exception being thrown, but accessing related collection displays "illegal access to loading collection". I'll paste relevant part of code here.

[Serializable]
[DataContract(IsReference = true)]
public partial class Department : Entity
{
    ...
    [DataMember]
    public virtual IList<PressJobRun> PressJobRun
    {
        get { return pressJobRunField; }
        protected set { pressJobRunField = value; }
    }
    ...
}

mapped as follows

public DepartmentMap()
{
    Id(x => x.Id);
    Map(x => x.Name)
        .Not.Nullable()
        .Length(100);
    HasMany(x => x.PressJobRun)
        .AsBag()
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .LazyLoad()
        .BatchSize(50);
    ...
}

I've also tried to disable lazy loading, by excluding the line and by calling .Not.LazyLoad(), however end is the same.

    using (var tx = m_Repository.Session.BeginTransaction())
    {
        var depts = m_Repository.Session.CreateCriteria<Department>().List<Department>();
        var dept = depts[0];
        ...
    }

I realize exposing Session is not the thing to do, but this was in attempts to make sure that session is open.

When I drill down the exception I see following stack trace:

   at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
   at NHibernate.Collection.AbstractPersistentCollection.Read()
   at NHibernate.Collection.AbstractPersistentCollection.ReadSize()
   at NHibernate.Collection.PersistentBag.get_Count()
   at NHibernate.DebugHelpers.CollectionProxy`1.get_Items()

The interesting gremlin comes here. I've set breakpoint on setter line of PressJobRun property.

  • if I step over it and quick watch pressJobRunField, I see the "illegal access to loading exception".
  • if I first quick watch value variable, I see the loaded collection. Stepping over setter line works as expected.

What I use

  • Visual Studio 2012, targeting .NET 4
  • NHibernate 3.3.1.400
  • SQL CE 4
  • Castle
  • Castle AutoTx facitlity
  • I manage session per WCF request myself

What I've tried

  • disabling lazy loading
  • made sure that session is open, and that it's same session during execution of offending code
  • toggled Inverted map of collection (thought I believe it should be inverted)
  • cleaned and rebuilt solution
  • made sure in Configuration Manager that all projects in solution are being built
  • set debugger to break on all thrown exceptions. Debugger doesn't break with exception that i see set in collection
1 Answers
Related