0

SQL:

    [FirstName] [nvarchar](250) NULL,
    [MiddleInitial] [nvarchar](250) NULL,
    [Surname] [nvarchar](250) NULL,

Mapping:

    public string    FirstName { get; set; }
    public string    MiddleInitial { get; set; }
    public string    Surname { get; set; }

    m.Map(x => x.FirstName).Length(255);
    m.Map(x => x.MiddleInitial).Length(255);
    m.Map(x => x.Surname).Length(255);

I need to add the Fullname field consisting of the [FirstName] + " " +[MiddleInitial]+ " " + [Surname]. (For using search in jqGrid)

Pls help me.

Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33

3 Answers3

1

Considering that you seem to be using this just on the front-end: How about a non-persiteable, read-only property?

private static string _fullNameFormat = "{0} {1} {2}";
public string FullName
{
    get
    {
        return string.Format(_fullNameFormat,this.FirstName,this.MiddleInitial,this.Surname);
    }
}

Untested, but should do it. In case NHibernate insists persisting the property, see here

The other, least invasive, alternative is to use an extension method for that Model

public static class ModelExtensions
{
    private static string _fullNameFormat = "{0} {1} {2}";
    public static string ToFullName(this Person person)
    {
        return string.Format(_fullNameFormat,person.FistName,person.MiddleInitial,person.Surname);
    }
}
Community
  • 1
  • 1
1

I do this with FORMULA

Map(x => x.Fullname).Formula("select FirstName+ ' ' + Surname + ' ' + MiddleInitial from Customer");

Its needed for ICriteria

Ihor Shubin
  • 10,628
  • 3
  • 25
  • 33
1

in this case you would compute the attribute in model:

public string Fullname { get { return string.Format("{0} {1} {2}", FirstName, MiddleInitial, Surname) .Replace(" "," ").Trim(); } }

Mehdi Payervand
  • 251
  • 3
  • 11