could not get a field value by reflection getter… nhibernate query cache with 2nd level cache

不问归期 提交于 2020-01-17 03:16:08

问题


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

回答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




回答2:


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?



来源:https://stackoverflow.com/questions/6372232/could-not-get-a-field-value-by-reflection-getter-nhibernate-query-cache-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!