流利的NHibernate:地图属性没有外键?

问题描述

| 我有这两节课:
public class Parent
{
    public virtual string Name { get; set; }
    public virtual string Family_id { get; set; }
}

public class Children
{
    public virtual string Name { get; set; }
    public virtual DateTime BirthDate { get; set; }
    public virtual string Family_id { get; set; }
}
当获取父对象时,我也想获取与父对象具有相同Family_id的最老(按BirthDate排序)的孩子。 数据库中的父级和子级之间没有外键。 (我不想使用两个不同的存储库,我想要映射中的功能) 我可以使用property-ref吗?     

解决方法

一种策略是在“儿童”集合上强制“急切加载”,并创建另一个属性来获取最大的孩子。 Property-Ref用于使用不是主键的列连接到另一个表。
public class Parent
{
    public virtual int Id {get; set;}
    public virtual string Name { get; set; }
    public virtual string Family_id { get; set; }
    public virtual Children OldestChild {
     get {
          return Children.OrderBy(x=>x.BirthDate).FirstOrDefault();
     }}
    public virtual IList<Children> Children {get; set;}
}

public class ParentMap : ClassMap<Parent>{
    public ParentMap(){
        Id(x=>x.Id);
        Map(x=>x.Name);
        HasMany(x=>x.Children).PropertyRef(\"Family_id\").Fetch.Join();
    }
}
另一种可能性是在父表(OldestChild_FK)中添加一列,然后从子表中加入该行。     ,我认为您想要做的是在父对象上创建一个名为OldestChild的属性或一个Oldest Children列表,然后忽略该属性并编写一些自定义查询(HQL或SQL)以返回所需的结果。 这是一个忽略FluentNhibernate中的属性的线程。     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...