Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can NHibernate set non-public properties?

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?

like image 871
Eric Anastas Avatar asked Dec 28 '22 06:12

Eric Anastas


1 Answers

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

like image 70
gcores Avatar answered Jan 11 '23 13:01

gcores