0

I got this exception

could not get a field value by reflection getter of DictionaryMaster.k__BackingField

with this inner exception:

Field '<>k__BackingField' defined on type is not a field on the target object which is of type 'System.Object[]'.

The problem exists only when i use eagerloading in query. Below i put my classes, relations and query.

public class DictionaryMaster
    {
        public virtual IList<DictionaryItem> DictionaryItems { get; private set; }
        public virtual System.String Code { get; private set; }
        public virtual System.String Description { get; private set; }
    }

   public class DictionaryMasterMap : ClassMap<DictionaryMaster>
    {
        public DictionaryMasterMap()
        {
            Cache.ReadOnly().Region("dictionary");
            LazyLoad();

            Id(x => x.Code) //i know this is so ugly
                .Column("DC_Code")
                .GeneratedBy.Assigned(); 
            Map(x => x.Description).Column("DC_Desc");
            HasMany(x => x.DictionaryItems)
                .Cascade.AllDeleteOrphan()
                .Fetch.Select()
                .AsBag()
                .Inverse()
                .Not.LazyLoad()
                .KeyColumns.Add("DI_DCCode");
        }
    }

 public class DictionaryItem
    {
        public virtual int Id { get; private set; }
        public virtual string Code { get; private set; }
        public virtual DictionaryMaster DictionaryMaster { get; private set; }
        public virtual string Description { get; private set; }
}

   public class DictionaryItemMap : ClassMap<DictionaryItem>
    {
        public DictionaryItemMap()
        {
            Cache.ReadOnly().Region("dictionary");

            Id(x => x.Id)
                .Column("DI_Id").GeneratedBy.Identity();

            Map(x => x.Code).Column("DI_Code");
            Map(x => x.Description).Column("DI_Desc");
            References(x => x.DictionaryMaster).Column("DI_DCCode");
        }
    }

Query:

session.Query<DictionaryMaster>()
                    .Fetch(x => x.DictionaryItems)
                    .Cacheable()
                    .CacheMode(CacheMode.Normal)
                    .ToList();
Adam Łepkowski
  • 2,048
  • 1
  • 24
  • 41

2 Answers2

1

I suspect many users are facing this problem - perhaps if you unmark your answer as the chosen answer the question will get more attention. AFAIK there's still no workaround which allows using Linq, Cacheable() and Fetch() at the same call.

This is meant as a comment, however probably because of my low SO ranking I can't create comments yet.

Cheers,

Jonno

Jonno
  • 1,976
  • 2
  • 21
  • 21
  • 1
    Look here http://stackoverflow.com/questions/7316931/2nd-level-cache-problem-with-join-query there is more about this error and solution – Adam Łepkowski Sep 23 '11 at 07:47
  • Thanks for the link, that's a great answer which saves quite a bit of headache. It's worth mentioning Dan Tao's answer in regards to the KeyEqualityComparer: http://stackoverflow.com/questions/98033/wrap-a-delegate-in-an-iequalitycomparer – Jonno Sep 24 '11 at 03:47
  • Thanks for link, I didn't see that entry before – Adam Łepkowski Sep 24 '11 at 21:54
0

I found what is wrong:

First: I really don't know why Fluent NHibernate maps my Id using FieldBacking, because I have property access.

Second: When I removed private modifier for the setter then it showed this exception:

Exception occurred getter of xxx'

The exception brought me to this page https://nhibernate.jira.com/browse/NH-2587. And now I am wondering about some workarounds. Any ideas?

Daniel Schilling
  • 4,829
  • 28
  • 60
Adam Łepkowski
  • 2,048
  • 1
  • 24
  • 41