在nhibernate中从INSERT命令中排除属性 [英] Exclude property from INSERT command in nhibernate

查看:104
本文介绍了在nhibernate中从INSERT命令中排除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有属性的实体,该属性希望是只读的-这意味着当我将此实体插入数据库时​​,SqlServer会自动生成该属性的值,因此我需要nhibernate在执行INSERT命令时忽略此属性,但要进行检索选择实体时使用它.

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.

重要说明:此属性不是ID!我不希望NHibernate使用生成器对其进行初始化,而SqlServer会自行完成.

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.

推荐答案

该功能受支持.有两个属性:

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:

摘录片段:

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

这篇关于在nhibernate中从INSERT命令中排除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆