could not get a field value by reflection getter…

2019-08-26 12:21发布

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();

2条回答
▲ chillily
2楼-- · 2019-08-26 13:17

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

查看更多
地球回转人心会变
3楼-- · 2019-08-26 13:22

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?

查看更多
登录 后发表回答