简体   繁体   English

NHibernate可以设置非公共属性吗?

[英]Can NHibernate set non-public properties?

Is it possible to setup NHibernate to load/persist a non-public property of a class? 是否可以设置NHibernate来加载/持久化类的非公共属性? For example I may have an Item class as follows. 例如,我可能有一个Item类,如下所示。

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. 但是我真的不希望我的Item类的使用者能够更改ItemID字段。 Can I restrict access to the set accessor of ItemID? 我可以限制对ItemID的set访问器的访问吗? If so what should I set it to? 如果是这样我应该将它设置为什么? Private, protected, internal, protected internal? 私人,受保护,内部,受保护的内部?

From the NHibernate tutorial: 从NHibernate教程:

Properties need not be declared public - NHibernate can persist a property with an internal, protected, protected internal or private visibility. 不需要将属性声明为公共 - NHibernate可以持久保存具有内部,受保护,受保护的内部或私有可见性的属性。

Just set the ItemID to private 只需将ItemID设置为私有

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

相关问题 Loquacious Nhibernate无法访问非公共属性进行映射 - Loquacious Nhibernate not able to access non-public properties for mapping ServiceStack OrmLite可以使用非公共setter设置属性吗? - Can ServiceStack OrmLite set a a property with a non-public setter? 如何使TableEntity序列化非公共属性? - How to make TableEntity serialize non-public properties? 通过反射获取类型的非公共属性 - Getting non-public properties of a type via reflection 将 appsetting.json 部分绑定到具有非公共属性或具有不匹配名称的属性的模型 - Bind appsetting.json section to a model with non-public properties or properties with non-matching names .NET 4.x 抱怨非公共属性的反序列化,而不是 .NET 3.5 - 如何解决? - .NET 4.x complains about deserialization of non-public properties as opposed to .NET 3.5 - how to fix it? 如何使用Entity Framework Code First映射非公共Collection属性? - How to map non-public Collection properties with Entity Framework Code First? SqlCommand 和 ExecuteReader 返回非公共成员 - SqlCommand and ExecuteReader return Non-Public Members C#CreateDocumentType用于非PUBLIC - C# CreateDocumentType for non-PUBLIC 在非公共字段上使用前缀 - Using a Prefix on Non-Public Fields
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM