[英]Simple FluentNHibernate parent/child mapping
New to FluentNHibernate =D FluentNHibernate = D的新手
I have a parent/children classes as follows: 我有一个父母/孩子类,如下所示:
public class Parent
{
public virtual int ID { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Child> Children { get; set; }
}
public class Child
{
public virtual int ID { get; private set; }
public virtual string Name { get; set; }
public virtual Parent ActiveParent { get; set; }
}
With mappings of: 具有以下内容的映射:
public ParentMap()
{
Id(x => x.ID);
Map(x => x.Name);
HasMany(x => x.Children)
.Inverse();
.Cascade.All();
}
public ChildMap()
{
Id(x => x.ID);
Map(x => x.Name);
//Map(x => x.ActiveParent)
// .Column(ParentID);
}
The commented out area of the child map is the question I'm currently having trouble with. 子地图的被注释掉的区域是我当前遇到的问题。 I'd like to be able to create a child object and call its' parent(ie, someChild.ActiveParent), but am unsure on how to map this via the fluent interface.
我希望能够创建一个子对象并调用其父对象(即someChild.ActiveParent),但是不确定如何通过流畅的界面来映射它。
The table structure for the child table holds a parentid, with the intent of lazy loading the parent object if called. 子表的表结构包含一个父代标识,目的是在调用时延迟加载父对象。 Any help is always greatly appreciated.
任何帮助总是很感激。
References(x => x.Parent);
Adding to mxmissile's answer, you will want to add a LazyLoad()
to the end of the References()
call, and also you might want to do something like this in your configuration: 添加到mxmissile的答案中,您将需要在
References()
调用的末尾添加一个LazyLoad()
,并且您可能还想在配置中执行以下操作:
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<ParentMap>()
.ConventionDiscovery.Add(ForeignKey.EndsWith("ID")))
The last line instructs Fluent NHibernate to expect foreign keys named like ParentID
rather than the default ( Parent_Id
?), so you no longer need to specify the column name explicitly in every relationship mapping. 最后一行指示Fluent NHibernate期望使用类似于
ParentID
外键,而不是默认键( Parent_Id
?),因此您不再需要在每个关系映射中显式指定列名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.