我在数据库中有以下设置:
Parent : Table
{
ParentId : int, PK
Name : varchar
}
Child : Table
{
ChildId : int, PK
ParentId : int, FK, unique
Name : varchar
}
我为我的网站设置了以下内容:
public class Parent
{
public virtual int ParentId {get;set;}
public virtual string Name {get;set;}
public virtual Child Child {get;set;}
}
public class Child
{
public virtual int ChildId {get;set;}
public virtual string Name {get;set;}
public virtual Parent Parent {get;set;}
}
我有以下ClassMaps(使用Fluent NHibernate):
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id(x => x.ParentId);
Map(x => x.Name);
HasOne(x => x.Child).PropertyRef(y => y.Parent);
}
}
public class ChildMap : ClassMap<Child>
{
public ChildMap()
{
Id(x => x.ChildId);
Map(x => x.Name);
References(x => x.Parent).Column("ParentId");
}
}
如果我使用会话并查询Child并获取Parent,则会在子项上设置Parent:
var query = from c in session.Query<Child>().Fetch(x => x.Parent)
select c; // c.Parent != null but c.Parent.Child == null
如果我使用会话并查询Parent和Fetch the Child,则在Parent上将Child设置为NULL:
var query = from p in session.Query<Parent>().Fetch(x => x.Child)
select p; // p.Child == null
任何人都知道我做错了什么?
更新:修复了ChildMap中的拼写错误