Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate: How to map an entire class as ReadOnly?

I have a few classes that read from very delicate tables, which is why I want them to be used by NHibernate as "ReadOnly". Establishing .ReadOnly() on each field map is really sloppy, and I'm not sure I trust it. How do I setup a class to be entirely readonly, as I can easily do with traditional XML mappings?

Edit: The answer does work. I expected it to throw an exception if I tried to save over a ReadOnly() object, but it just silently does so.

Thanks.

like image 829
rebelliard Avatar asked May 20 '10 05:05

rebelliard


2 Answers

With Fluent NHibernate, it's as simple as:

class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        ReadOnly();

        // Mappings
    }
}
like image 76
Bryan Menard Avatar answered Oct 14 '22 04:10

Bryan Menard


The ReadOnly() property actually does NOT work like you would expect. Using this property makes sure that the objects that you retrieve are read-only, so you cannot UPDATE them. However, it does NOT prevent the creation of new records or even the deletion of existing records in the database!

like image 16
Roger Frehen Avatar answered Oct 14 '22 05:10

Roger Frehen