也许你正在寻找别的东西
public class Fund
{
public virtual int Id { get; set; }
public virtual Country Country { get; set; }
public virtual Config Config { get; set; }
public virtual IDictionary<Config, FundPerformance> Performances { get; private set; }
public virtual FundPerformance ActualPerformance { get { return Performances[Config]; } }
}
public class FundPerformance
{
public virtual Fund Fund { get; set; }
public virtual Config Config { get; set; }
public virtual int OneMonth { get; set; }
public virtual int ThreeMonth { get; set; }
public virtual int YTD { get; set; }
}
public class Config
{
public virtual int Id { get; set; }
}
public class FundMap : ClassMap<Fund>
{
public FundMap()
{
CompositeId()
.KeyProperty(x => x.Id, "FundId")
.KeyProperty(x => x.CountryId, "FundCountryId");
HasMany(x => x.Performances)
.KeyColumns.Add("FundPerformanceStatistics_FundId", "FundPerformanceStatistics_FundCountryId")
.AsMap(fp => fp.Config)
.Cascade.AllDeleteOrphan()
.Component(c =>
{
c.ParentReference(x => x.Fund);
c.References(x => x.Config, "FundPerformanceStatistics_ConfigId");
c.Map(x => x.OneMonth, "FundPerformanceStatistics_OneMonthBack");
c.Map(x => x.ThreeMonth, "FundPerformanceStatistics_ThreeMonthBack");
c.Map(x => x.YTD, "FundPerformanceStatistics_YearToDate");
})
}
}
// Query
var someCountry = session.Load<Country>(5); <- get the Country with id 5 without going to the db (proxy when not already loaded) only to have the reference
var key = new Fund { Id = 1, Country = someCountry }
var fund = session.Get<Fund>(key);
var performanceByConfig = fund.Performances[_config];
原答案:
如果 Fund 有一个复合键,那么 FundPerformance 也应该有一个。另外我认为对 Fund 的引用更像是 OOP:
public class FundPerformance
{
public virtual Fund Fund { get; set; }
public virtual int OneMonth { get; set; }
public virtual int ThreeMonth { get; set; }
public virtual int YTD { get; set; }
}
要么将其映射为组件(因为它看起来像依赖的东西)
public class FundMap : ClassMap<Fund>
{
public FundMap()
{
CompositeId()
.KeyProperty(x => x.Id, "FundId")
.KeyProperty(x => x.CountryId, "FundCountryId")
.KeyProperty(x => x.ConfigId, "ConfigId");
HasMany(x => x.Performances)
.KeyColumns.Add("FundPerformanceStatistics_FundId", "FundPerformanceStatistics_FundCountryId", "FundPerformanceStatistics_ConfigId")
.Cascade.AllDeleteOrphan()
.Component(c =>
{
c.ParentReference(x => x.Fund);
c.Map(x => x.OneMonth, "FundPerformanceStatistics_OneMonthBack");
c.Map(x => x.ThreeMonth, "FundPerformanceStatistics_ThreeMonthBack");
c.Map(x => x.YTD, "FundPerformanceStatistics_YearToDate");
})
}
}
或作为具有复合键的实体
public class FundPerformanceMap : ClassMap<FundPerformance>
{
public FundPerformanceMap()
{
CompositeId()
.KeyReference(x => x.Fund, param => param
.Column("FundPerformanceStatistics_FundId")
.Column("FundPerformanceStatistics_FundCountryId")
.Column("FundPerformanceStatistics_ConfigId"));
Map(x => x.OneMonth).Column("FundPerformanceStatistics_OneMonthBack");
Map(x => x.ThreeMonth).Column("FundPerformanceStatistics_ThreeMonthBack");
Map(x => x.YTD).Column("FundPerformanceStatistics_YearToDate");
}
}
// Query
var key = new Fund { Id = 1, CountryId = 2, ConfigId = 3}
var fund = session.Get<Fund>(key);
var performances = fund.Performances;