当我尝试映射嵌套的父子关系时,我遇到了 FluentNHibernate 的问题。当我尝试这样的查询时:
var courier = session.Query<Courier>().FirstOrDefault(x => x.Code == "X");
CourierPrice 中的 DistributionCenter 属性包含所有可能的快递员的 CourierPrice 实体,而不仅仅是 X。是否可以在不修改映射的情况下更改此行为?
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; }
}
映射:
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");
}