问题描述
我有个小问题与NHibernate(流利)我有两个对象,一个包含另一个 - 父母和一个孩子(预定义的对象,只读)
I have small problem with nHibernate (fluent) I have two objects, one contains another - a parent and a child (predefined objects, readonly).
映射:
public class ParentClass
{
public virtual int Id { get; set; }
public virtual ChildClass Metoda { get; set; }
}
public ParentClassMap() {
Table("Wyceny");
Id(x => x.Id).Column("Id").GeneratedBy.TriggerIdentity();
References(x => x.Metoda).Column("RMW_ID");
}
public ChildClass
{
public virtual int Id { get; set; }
public virtual string Nazwa { get; set; }
}
public ChildClassMap()
{
Table("Metody");
Id(x => x.Id).Column("Id");
Map(x => x.Nazwa).Column("Nazwa_met");
}
一切完美,直到我选择的子对象使用id = 0,阅读仍然有效为ID = 0,但是当我试图保存或正确ChildObject(从数据库通过NHibernate的前面readed)更新父,NHibernate的店空,而不是价值。
Everything works perfectly until I chose child object with id = 0, reading still works for id=0, but when I'm trying to save or update Parent with correct ChildObject(readed previously from db through nHibernate), nHibernate stores null instead of value.
有什么建议么?
NHibernate的3.3.1.4000
流利1.4.0.0
nHibernate 3.3.1.4000fluent 1.4.0.0
推荐答案
这里的问题将是未保存价值
。 NHibernate的必须决定是否与对象操作将被插入或更新。这一决定来自未保存价值
设置,默认情况下为 INT
设置为0。
The issue here would be the unsaved-value
. NHibernate must decide if operations with object will be insert or update. This decision comes from unsaved-value
setting, which is by default for int
set to 0.
尽量延长您的映射 ChildClass
:
public ChildClassMap()
{
Table("Metody");
Id(x => x.Id)
.Column("Id")
.UnsavedValue(-1);
...
请参阅的,举:
未保存价值
的(可选 - 默认为一个明白人的值)的:一个特定的标识属性值,表示一个实例是刚刚创建的(未保存)的,和已经在一个保存或装载的实例区分开来以前的会话。
这是很好的通过亚当酒吧的(文章的后半部分)的
And here is nice Id mapping overview by Adam Bar (the second half of the article)
这篇关于NHibernate的商店ID = 0为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!