views:

25

answers:

1

Is it possible to setup NHibernate to load/persist a non-public property of a class? For example I may have an Item class as follows.

public class Item
{
    public int ItemID {get; set;}
    public string Name{get; set;}
}

With the following mapping:

<class name="RCL.Item" table="Items">
    <id name="ItemID" type="Int32" column="ItemID">
        <generator class="native"/>
    </id>
    <property name="Author" />
</class>

However I really don't want the consumers of my Item class to be able to change the ItemID field. Can I restrict access to the set accessor of ItemID? If so what should I set it to? Private, protected, internal, protected internal?

+5  A: 

From the NHibernate tutorial:

Properties need not be declared public - NHibernate can persist a property with an internal, protected, protected internal or private visibility.

Just set the ItemID to private

gcores
Humm, ok that was stupid easy. But how can NHibernate see my private accessors?
Eric Anastas
Using reflection
gcores