Fluent NHibernate:没有外键的映射属性?

发布于 2024-11-09 00:21:28 字数 481 浏览 1 评论 0原文

我有这两个类:

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 的最年长(按出生日期排序)的子级。

数据库中的父级和子级之间没有外键。

(我不想使用两个不同的存储库,我想要映射中的功能)

我可以使用 property-ref 吗?

I have these two classes:

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; }
}

When I fetch a parent, I also want to fetch the oldest (ordered by BirthDate) children that has the same Family_id as the parent.

There is no foreign key between the parent and the children in the database.

(I do not want to use two different repositories, I want the functionality in the mappings)

Is property-ref something I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2024-11-16 00:21:28

一种策略是对 Children 集合强制急切加载,并创建另一个属性来获取最大的孩子。

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) 添加一列,然后从子表中加入该行。

One strategy would be to force an Eager Load on a Children collection and create another property to get the oldest child.

Property-Ref is used to join to another table using a column which is not the primary key.

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();
    }
}

Another possibility would be to add a column to the Parent table (OldestChild_FK) and then join in that row from the Children table.

三月梨花 2024-11-16 00:21:28

我认为您想要做的是在父级上创建一个名为 OldestChild 的属性或最古老的子级列表,并忽略该属性并编写一些自定义查询(HQL 或 SQL)以返回您想要的结果。

这里是一个关于忽略的线程FluentNhibernate 中的属性。

I think what you want to do is create a property on the Parent called OldestChild or a list of Oldest Children and ignore that property and write some custom query (HQL or SQL) to return the results you want.

Here is a thread on ignoring properties in FluentNhibernate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文