无法通过反射 getter 获取字段值...带有二级缓存的 nhibernate 查询缓存
我得到了这个异常
无法通过以下方式获取字段值 反射吸气剂 DictionaryMaster.
k__BackingField
具有以下内部异常:
字段“<>k__BackingField”定义于 类型不是目标上的字段 类型为的对象 'System.Object[]'。
仅当我在查询中使用预加载时才存在问题。下面我放置了我的类、关系和查询。
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");
}
}
询问:
session.Query<DictionaryMaster>()
.Fetch(x => x.DictionaryItems)
.Cacheable()
.CacheMode(CacheMode.Normal)
.ToList();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑许多用户都面临这个问题 - 也许如果您取消将答案标记为所选答案,那么该问题会得到更多关注。 AFAIK 仍然没有允许在同一调用中使用 Linq、Cacheable() 和 Fetch() 的解决方法。
这是一条评论,但可能由于我的排名较低,我还无法创建评论。
干杯,
乔诺
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
我发现问题出在哪里:
首先:我真的不知道为什么 Fluent NHibernate 使用 FieldBacking 映射我的 Id,因为我有属性访问权限。
第二:当我删除 setter 的私有修饰符时,它显示了以下异常:
异常将我带到此页面 https://nhibernate.jira.com/浏览/NH-2587。现在我想知道一些解决方法。有什么想法吗?
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:
The exception brought me to this page https://nhibernate.jira.com/browse/NH-2587. And now I am wondering about some workarounds. Any ideas?