Reputation: 390
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");
}
Upvotes: 1
Views: 1657
Reputation: 131
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
Upvotes: 1
Reputation: 49251
The query is returning the correct results. The Distributioncenter.Courierprices collection should contain all Courierprices that are linked to the Distributioncenter.
Upvotes: 1