流利的nHibernate - 用组合键映射儿童产生空引用 [英] Fluent nHibernate - Mapping Children with Composite Keys Yielding Null References
问题描述
$ $ p $ $ $ $ $ $ $ $变种父母=新父母{
Children = new List< ; Child> {
new Child {
Other = otherReference
}
}
};
甚至使用 Add()
方法...
lockquote
parent.Children.Add(new Child {Other = other});
对父
的引用不会被传入。它只是作为一个空属性结束。
{不能将值NULL插入到'ParentId'列'mssql_test.Children'列中}
I can 这个... 但这似乎有点多余。我的理解是,它应该能够自己推断参考。如果这是不可能的,也许我只是误解。谁能帮我?
$ p $ new Child {
Parent = parentReference,
Other = otherReference
}
类
class父母{
int Id {get;组; }
IList< Child>孩子{get;组; }
}
class其他{
int Id {get;组; }
}
class Child {
父Parent {get;组; }
其他其他{get;组; }
//其他属性
}
映射
ChildMap(){
CompositeId()
.KeyReference(x => x.Parent,ParentId)
.KeyReference(x => x.Other,OtherId);
ParentMap(){
HasManyToMany(x => x.Children)
.AsBag()
.ChildKeyColumns.Add(new [ ] {ParentId,OtherId})
.Inverse()
.Cascade.All())
.Table([Test]);
$ / code $ / $ p
解决方案
问题是,您应该将HasMany映射为HasManyToMany的子集合。这是映射应该看起来如何:
$ Child $($)
CompositeId()//这是很好的$ b $ .KeyReference(x => x.Parent,ParentId)
.KeyReference(x => x.Other,OtherId);
ParentMap(){//这是修正
HasMany(c => c.Children)
.Inverse()
。 Cascade.All()
.KeyColumn(ParentId)//如果父Id标示映射到ParentId不需要col
.Table([Test]);
}
Given a simple parent -> child (CK,CK) setup like this .. I am having trouble with adding a new child object and it getting the parent reference. So I would add the object in such a way ..
var parent = new Parent{
Children = new List<Child>{
new Child{
Other = otherReference
}
}
};
Or even adding it using the Add()
method...
parent.Children.Add(new Child { Other = other });
The reference to the Parent
does not get pushed through. It just ends up as a null property. I get the following exception.
{"Cannot insert the value NULL into column 'ParentId', table 'mssql_test.Children'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
I can do this ...
new Child {
Parent = parentReference,
Other = otherReference
}
But that seems a bit redundant. My understanding is that it should be able to infer the reference by itself. If this is not possible, perhaps I am just misunderstanding. Can anyone help me? I have an outline of my code below.
Classes
class Parent {
int Id { get; set; }
IList<Child> Children { get; set; }
}
class Other {
int Id { get; set; }
}
class Child {
Parent Parent { get; set; }
Other Other { get; set; }
// other properties
}
Mapping
ChildMap() {
CompositeId()
.KeyReference(x => x.Parent, "ParentId")
.KeyReference(x => x.Other, "OtherId");
}
ParentMap(){
HasManyToMany(x => x.Children)
.AsBag()
.ChildKeyColumns.Add(new[] { "ParentId", "OtherId" })
.Inverse()
.Cascade.All())
.Table("[Test]");
}
As @KeithS points out the problem is that you have mapped the Child collection as HasManyToMany when it should be HasMany. Here is how the mapping should look:
ChildMap() {
CompositeId() //This is is good
.KeyReference(x => x.Parent, "ParentId")
.KeyReference(x => x.Other, "OtherId");
}
ParentMap(){ //This is the fix
HasMany(c => c.Children)
.Inverse()
.Cascade.All()
.KeyColumn("ParentId") //Not needed if Parent Id prop maps to ParentId col
.Table("[Test]");
}
这篇关于流利的nHibernate - 用组合键映射儿童产生空引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!