也許你正在尋找其他的東西
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];
原來的答覆:
如果基金有compositeKey然後FundPerformance應該有一個也是。此外,我認爲一個參考基金更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;
Hi @Firo,謝謝你。對不起,我現在纔回來,但今天大部分時間我都被其他東西纏住了。儘管如此,我有點困惑。你已經說過'Fund'應該有'CompositeId'映射來包含'ConfigId'。我不確定這會起作用,因爲基金錶沒有ConfigId列。它只在FundPerformance中。這會起作用嗎?或者我可以只指定一個ID和CountryID的Fund CompositeId映射嗎? – DaveDev
你的第一句話:「我有一個基金清單,每個基金都有一個ID,ConfigID和一個CountryID。」讓我想你有。那麼在Fundperformance中configId的目的是什麼? – Firo
對不起 - 我不打算讓它混淆。 FundPerformance中的ConfigID與計算性能的方式有關。基金有一個財產配置ID,用於確定它應該選擇哪種績效。 – DaveDev