Fluent NHibernate: a card property without a foreign key?

I have 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 take a parent, I also want to get the oldest (ordered by BirthDate) children who have the same Family_id as the parent.

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

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

Is property-ref something I can use?

+3
source share
2 answers

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

- , .

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), "".

+2

, OldestChild - (HQL SQL), .

- FluentNhibernate.

+1

All Articles