开发者

NHibernate helper properties in entity

开发者 https://www.devze.com 2023-02-09 16:14 出处:网络
I have an NHibernate entity that looks like this: public class Offender { public virtual string FName { get; set; }

I have an NHibernate entity that looks like this:

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 convenien开发者_开发百科ce property, and it isn't in the database. But NHibernate doesn't like the property being there and throws this exception:

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?


NHibernate needs all properties to be virtual...even "fake" properties like your "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. 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.


You can also use a formula in your fluent mappings:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消