更新时间:2023-09-23 23:17:04
我添加了以下项目:
I've added the following items:
子类:字段_parent,构造函数
父类:AddChild方法
子映射:用于父级属性的多音
父级映射:inverse(true)使子级成为父级处理程序
Child class: a field _parent, a constructor
Parent class: AddChild method
Child mapping: manytoone for parent property
Parent mapping: inverse(true) to make parent handler of the children
完整代码:
public class Child
{
int Id;
string Name;
Parent _parent;
public Child(Parent parent)
{
_parent = parent;
}
}
public class Parent
{
int Id;
string Name;
Iesi.Collections.Generic.ISet<Child> Children;
public virtual Child AddChild()
{
Child newChild = new Child(this); //link parent to child via constructor
Children.Add(newChild); //add child to parent's collection
return newChild; //return child for direct usage
}
}
public ChildMapping()
{
Table("Children");
Id(p => p.Id, m => {
m.Column("Id");
m.Generator(Generators.Identity);
});
Property(p => p.Name, m => {
m.Column("Name");
m.NotNullable(true);
});
ManyToOne(x => x.Parent, map => {
map.Column("Id"); /* id of the parent table */
map.Access(Accessor.Field);
map.NotNullable(true);
map.Class(typeof(Parent));
});
}
public ParentMapping()
{
Table("Parents");
Id(p => p.Id, m => {
m.Column("Id");
m.Generator(Generators.Identity);
});
Property(p => p.Name, m => {
m.Column("Name");
m.NotNullable(true);
});
Set(p => p.Children, m => {
m.Cascade(Cascade.All | Cascade.DeleteOrphans);
m.Key(k => {
k.Column("Id"); /* id of the child table */
k.NotNullable(true);
k.Inverse(true); /* makes the parent handle it's childlist */
});
}, a => a.OneToMany());
}
应该起作用.