简体   繁体   English

流畅的NHibernate:如何将整个类映射为ReadOnly?

[英]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". 我有一些类从非常精细的表中读取,这就是为什么我希望它们被NHibernate用作“ReadOnly”。 Establishing .ReadOnly() on each field map is really sloppy, and I'm not sure I trust it. 在每个字段映射上建立.ReadOnly()非常草率,我不确定我是否相信它。 How do I setup a class to be entirely readonly, as I can easily do with traditional XML mappings? 我如何设置一个完全只读的类,因为我可以轻松地使用传统的XML映射?

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. 我期望它在我试图保存ReadOnly()对象时抛出异常,但它只是默默地这样做。

Thanks. 谢谢。

With Fluent NHibernate, it's as simple as: 使用Fluent NHibernate,它很简单:

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

        // Mappings
    }
}

The ReadOnly() property actually does NOT work like you would expect. ReadOnly()属性实际上并不像您期望的那样工作。 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! 但是,它不会阻止创建新记录甚至删除数据库中的现有记录!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM