Fluent nHibernate 映射问题
我有这些表/类(示例):
table FirstTable (
Id INTEGER NOT NULL DEFAULT AUTOINCREMENT,
Name VARCHAR(100) NOT NULL,
Document VARCHAR(20) NOT NULL
)
table SecondTable (
Id INTEGER NOT NULL,
Something VARCHAR(100) NULL,
FOREIGN KEY (Id) REFERENCES FirstTable (Id)
)
public class FirstClass {
public string Name { get; set; }
public string Document { get; set; }
public SecondClass SecondClass { get; set; }
}
public class SecondClass {
public string Something { get; set; }
public FirstClass FirstClass { get; set; }
}
映射是:
public class FirstClassMap : ClassMap<FirtsClass> {
Table("FirstTable");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
Map(x => x.Document);
References(x => x.SecondClass, "Id").ForeignKey();
}
public class SecondClassMap : ClassMap<SecondClass> {
Table("SecondTable");
Id(x => x.Id).GeneratedBy.Foreign("FirstClass");
Max(x => x.Something);
HasOne(x => x.FirstClass).PropertyRef(x => x.SecondClass).Cascade.SaveUpdate();
}
FirstClass 可以有 (0,1) SecondClass,SecondClass 可以有 (1,1) FirstClass。
以下代码返回错误“尝试从 null 一对一属性分配 id:SecondClass”
var test = new SecondClass();
test.FirstClass = new FirstClass();
test.Something = "New test";
test.FirstClass.Name = "My name";
test.FirstClass.Document = "My document";
// ... commands to save.
I have these tables/classes (example):
table FirstTable (
Id INTEGER NOT NULL DEFAULT AUTOINCREMENT,
Name VARCHAR(100) NOT NULL,
Document VARCHAR(20) NOT NULL
)
table SecondTable (
Id INTEGER NOT NULL,
Something VARCHAR(100) NULL,
FOREIGN KEY (Id) REFERENCES FirstTable (Id)
)
public class FirstClass {
public string Name { get; set; }
public string Document { get; set; }
public SecondClass SecondClass { get; set; }
}
public class SecondClass {
public string Something { get; set; }
public FirstClass FirstClass { get; set; }
}
The mapping is:
public class FirstClassMap : ClassMap<FirtsClass> {
Table("FirstTable");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Name);
Map(x => x.Document);
References(x => x.SecondClass, "Id").ForeignKey();
}
public class SecondClassMap : ClassMap<SecondClass> {
Table("SecondTable");
Id(x => x.Id).GeneratedBy.Foreign("FirstClass");
Max(x => x.Something);
HasOne(x => x.FirstClass).PropertyRef(x => x.SecondClass).Cascade.SaveUpdate();
}
FirstClass can have (0,1) SecondClass, and SecondClass can have (1,1) FirstClass.
The bellow code return the error "attempted to assign id from null one-to-one property: SecondClass"
var test = new SecondClass();
test.FirstClass = new FirstClass();
test.Something = "New test";
test.FirstClass.Name = "My name";
test.FirstClass.Document = "My document";
// ... commands to save.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NH 似乎试图先保存 SecondClass,但无法从尚未保存的 FirstClass 中获取生成的 ID。
尝试将
.Cascade.SaveUpdate()
移至FirstClassMap
中的References
声明,并在 FirstClass 上调用保存命令。It seems like NH is trying to save SecondClass first and fails to grab a generated ID from the not yet saved FirstClass.
Try to move the
.Cascade.SaveUpdate()
to theReferences
declaration inFirstClassMap
and call the save command on FirstClass.