简体   繁体   English

实体中的NHibernate辅助属性

[英]NHibernate helper properties in entity

I have an NHibernate entity that looks like this: 我有一个NHibernate实体,如下所示:

public class Offender
{
    public virtual string FName { get; set; }
    public virtual string MName { get; set; }
    public virtual string LName { get; set; }

    public string FullName
    {
        get
        {
            return FName + " " + MName + " " + LName;
        }
    }
}

Fullname is a convenience property, and it isn't in the database. Fullname是一个便利的属性,它不在数据库中。 But NHibernate doesn't like the property being there and throws this exception: 但NHibernate不喜欢该属性并抛出此异常:

The following types may not be used as proxies:
mPSOR.Data.Entities.SORPerson: method get_FullName should be 'public/protected virtual' or 'protected internal virtual'

Is there any way to include a helper property like that? 有没有办法包括这样的帮助器属性? Or do I have to put computation like that when compiling a DTO or in my view? 或者在编译DTO时或在我的视图中是否必须进行类似的计算?

NHibernate needs all properties to be virtual...even "fake" properties like your "FullName". NHibernate需要所有属性都是虚拟的...甚至像“FullName”这样的“假”属性。

Just make it virtual and it will work: 只需将其设为虚拟即可使用:

public virtual string FullName
{
}

Go ahead and make the method virtual anyway. 无论如何,继续使方法成为虚拟。 NHibernate is complaining because part of its functionality is to create proxies that it uses to provide lazy-loading. NHibernate抱怨是因为它的一部分功能是创建它用来提供延迟加载的代理。 If it cannot create a proxy that has the same functionality, but with overrides that initialize the actual object, then it can't work transparently. 如果它无法创建具有相同功能但具有初始化实际对象的覆盖的代理,则它无法透明地工作。

您还可以在流畅的映射中使用公式:

Map(x => x.LName).Formula("FName + ' ' + MName + ' ' + LName");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM