Domanda

I have a problem with FluentNHibernate when I try to map a nested parent-child relationship. When I try a query like this:

var courier = session.Query<Courier>().FirstOrDefault(x => x.Code == "X"); 

the DistributionCenter property in CourierPrice contains CourierPrice entities for all possible couriers, not only X. Is it possible to change this behavior without modifying the mappings?

public class Courier
{
  public Courier()
  {
    Courierprices = new List<Courierprice>();
  }
  public virtual System.Guid Guid { get; set; }
  public virtual string Code { get; set; }
  public virtual IList<Courierprice> Courierprices { get; set; }}
}
public class Courierprice 
{
  public virtual System.Guid Guid { get; set; }
  public virtual Courier Courier { get; set; }
  public virtual Distributioncenter Distributioncenter { get; set; }
  public virtual decimal? Price { get; set; }
}
public class Distributioncenter
{
  public Distributioncenter()
  {
    Postcodes = new List<Postcode>();
  }
  public virtual System.Guid Guid { get; set; }
  public virtual string Code { get; set; }
  public virtual IList<Courierprice> Courierprices { get; set; }
}

Mapping:

public CourierMap()
{
  Table("Couriers");
  LazyLoad();
  Id(x => x.Guid).GeneratedBy.Assigned().Column("Guid");
  Map(x => x.Code).Column("Code");
  HasMany(x => x.Courierprices).KeyColumn("Courier");
}
public CourierpriceMap()
{
  Table("CourierPrices");
  LazyLoad();
  Id(x => x.Guid).GeneratedBy.Assigned().Column("Guid");
  References(x => x.Courier).Column("Courier");
  References(x => x.Distributioncenter).Column("DistributionCenter");
  Map(x => x.Price).Column("Price");
}
public DistributioncenterMap()
{
  Table("DistributionCenters");
  LazyLoad();
  Id(x => x.Guid).GeneratedBy.Assigned().Column("Guid");
  Map(x => x.Code).Column("Code");
  HasMany(x => x.Courierprices).KeyColumn("DistributionCenter");
}
È stato utile?

Soluzione

The query is returning the correct results. The Distributioncenter.Courierprices collection should contain all Courierprices that are linked to the Distributioncenter.

Altri suggerimenti

I made a simple app with the mappings provided and i found a flaw in them in the DistributioncenterMap you used

Id(x => x.Guid).GeneratedBy.Assigned().Column("Guid");

instead of

Id(x => x.Guid).GeneratedBy.Guid().Column("Guid");

i added some rows in a test run two of them were Code == "X" and it got me with the first one with no problems. give it a try

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top