[英]fluent nhibernate mappings that save in both directions
我希望能够做到以下几点:
var child = new Child {
Name = "foo",
Parent = new Parent { Name = "bar" }
};
session.Save(child);
要么
var parent = new Parent {
Name = "foo",
Children = new List<Child> {
new Child { Name = "bar" },
new Child { Name = "baz" },
},
};
session.Save(parent);
并且在两种情况下都可以在调用Save()
时Save()
所有创建的对象。 这是课程。 我不知道如何映射Parent
和Child
之间的一对多关系,这将有助于在一次调用Save()
保存所有创建的对象。 我希望可以使用任何一个版本,但在任何给定时间只能使用一个版本。 这背后的基本原理是我正在创建测试数据构建器,在某些情况下,从Child
端更容易连接它,在其他情况下,从Parent
端更容易连接。 因此,为什么我希望能够在给定任一对象的情况下在两个方向上级联插入和删除。 我不关心更新。 这只会用于插入数据,对应用程序运行我的测试,然后删除数据。
以下是涉及的课程:
public class Child {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Parent Parent { get; set; }
}
public class Parent {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IEnumerable<Child> Children { get; set; }
}
public ChildMap : ClassMap<Child> {
public ChildMap() {
Id(x => x.Id);
Map(x => x.Name);
// How should I set this up?
Reference(x => x.Parent);
}
}
public ParentMap : ClassMap<Parent> {
public ParentMap() {
Id(x => x.Id);
Map(x => x.Name);
// How should I set this up ?
HasMany(x => x.Children);
}
}
为了能够从两侧保存级联,我们可以使用级联设置
所以,这种映射就足够了:
public ChildMap()
{
...
BatchSize(100); // this is the way how to solve 1 + N (good practice, mine at least)
// How should I set this up?
References(x => x.Parent) // References, not Reference
.Cascade.SaveUpdate()
;
}
public ParentMap()
{
...
BatchSize(100); // this is the way how to solve 1 + N
// How should I set this up ?
HasMany(x => x.Children)
.BatchSize(100) // no 1 + N
.Cascade.AllDeleteOrphan()
.Inverse()
;
有了这些设置,上面的方法就可以了,有一个重要的变化 - 孩子们必须知道它的父母。 因为我们使用逆...
这将有效:
var child = new Child
{
Name = "foo",
Parent = new Parent {Name = "bar"}
};
session.Save(child);
session.Flush();
session.Clear();
var persistedChild = session.Get<Child>(child.ID);
这也将(但设置child.Parent参考)
var parent = new Parent
{
Name = "foo",
Children = new List<Child>
{
new Child {Name = "bar"},
new Child {Name = "baz"},
},
};
foreach (var ch in parent.Children)
{
ch.Parent = parent; // we MUST do this, there is .Inverse()
}
session.Save(parent);
session.Flush();
session.Clear();
var persistedParent = session.Get<Parent>(parent.ID);
在这里阅读更多有关批量大小的信息 (不是问题的一部分,但我总是使用它)
关于逆映射和引用赋值的一些其他细节
而且,为什么我们不能在.Rferences()(多对一)上使用相同的级联NHibernate多对一级联
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.