No persister for: Castle.Proxies.<EntityName>Proxy and lazy=“true” in NHibernate?

Deadly 提交于 2019-12-08 19:31:48

问题


I am trying to use lazy loading for a property of one of my entities

The property mapping is something like this:

<property name="Foobar" type="AnsiString" column="FOOBAR" lazy="true"/>

However when I am tring to save an instance of this entities (using Linq), it throws a DatabaseQueryException with the following inner exception:

NHibernate.MappingException: No persister for: Castle.Proxies.FooEntityProxy"

And when I remove the lazy="true" item, the exception doesn't get thrown anymore. What is the problem with using lazy="true" and how to fix this?


回答1:


I know this is a late answer, but I had this same problem earlier. I was using a custom interceptor to create the proxies, and so I found that the issue was that I hadn't overridden the "GetEntityName" method. Analyzing the proxy within the GetEntityName method, and returning the proper class name did the trick.

In my case, I had a simple extension method to unproxy my objects, called "UnProxy", so my whole implementation of this method looked like this:

public override string GetEntityName(object entity)
{
    if (entity == null)
        return base.GetEntityName(entity);
    return entity.UnProxy().GetType().FullName;
}



回答2:


Are you sure you are using NHibernate 3? I think only this version supports scalar properties lazy loading!

update

Not sure if it can help you but try to look here:

NHibernate lazy loading property - what does build-time bytecode instrumentation mean?

or here:

NHibernate lazy properties behavior?




回答3:


If you mark a property as lazy, it must be a virtual automatic property (with no body like public virtual MyType Baz { get; set; }). If you attempt to access the underlying field value, instead of going through the property, you will circumvent the lazy loading of the property, and may get unexpected results.



来源:https://stackoverflow.com/questions/6434415/no-persister-for-castle-proxies-entitynameproxy-and-lazy-true-in-nhibernate

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