Fluent NHibernate Map to private/protected Field that has no exposing Property

Posted by Jon Erickson on Stack Overflow See other posts from Stack Overflow or by Jon Erickson
Published on 2010-04-23T22:54:03Z Indexed on 2010/04/23 23:03 UTC
Read the original article Hit count: 281

I have the following Person and Gender classes (I don't really, but the example is simplified to get my point across), using NHibernate (Fluent NHibernate) I want to map the Database Column "GenderId" [INT] value to the protected int _genderId field in my Person class. How do I do this?

FYI, the mappings and the domain objects are in separate assemblies.

public class Person : Entity
{
    protected int _genderId;

    public virtual int Id { get; private set; }
    public virtual string Name { get; private set; }

    public virtual Gender Gender
    {
        get { return Gender.FromId(_genderId); }
    }
}

public class Gender : EnumerationBase<Gender>
{
    public static Gender Male
        = new Gender(1, "Male");

    public static Gender Female
        = new Gender(2, "Female");

    private static readonly Gender[] _genders
        = new[] { Male, Female };

    private Gender(int id, string name)
    {
        Id = id;
        Name = name;
    }

    public int Id { get; private set; }
    public string Name { get; private set; }

    public static Gender FromId(int id)
    {
        return _genders.Where(x => x.Id == id).SingleOrDefault();
    }
}

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate