Exclude property from INSERT command in nhibernate

笑着哭i 提交于 2019-12-31 02:28:12

问题


I have an entity with a property which I wish to be readonly - meaning that when I insert this entity to the DB, SqlServer will generate the property's value automatically so I need nhibernate to ignore this property when executing the INSERT command but retrieve it when selecting the entity.

Important note: this property isn't ID! I don't want NHibernate to initialize it using generator, SqlServer will do it by itself.

And another note: I use configuration mapping so no fluent mapping solutions please.


回答1:


That functionality is supported. There are two attributes:

<property name="GeneratedBySql" insert="false" update="false" />

The same could be applied even for reference mapping

<many-to-one name="ReferenceGeneratedBySql" insert="false" update="false" />

If we want to use Mapping-by-Code we do have the same in places, see:

Mapping-by-Code - Property (by Adam Bar)

Snippet cited:

Property(x => x.Property, m =>
{
    m.Column("columnName");
    ...
    m.Update(false);
    m.Insert(false);


来源:https://stackoverflow.com/questions/26229562/exclude-property-from-insert-command-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!