如何使用 Fluent NHibernate 和 QueryOver 查询复合主键?
我有一个 Funds
列表,每个基金都有一个 ID
、ConfigID
和 CountryID
。我需要根据这些 ID 为每个Fund
选择FundPerformances
。
FundPerformance
表具有一个由 ID
、ConfigID
和 ID
组成的复合主键。 国家ID
。
到目前为止,我
perfs = _session.QueryOver<FundPerformance>()
.Where(xx => xx.ConfigId == _configId)
.Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.Id), funds.Select(xx => xx.Id).ToArray()))
.Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.CountryId), funds.Select(xx => xx.CountryId).ToArray()))
.List<FundPerformance>().ToList();
认为这不会起作用,因为它是在单个字段上进行选择,而不是在复合 ID 上进行选择。
我需要做什么才能正确选择数据?
这是当前的 FundPerformance 映射类:
public class FundPerformanceMap : ClassMap<FundPerformance>
{
public FundPerformanceMap()
{
Id(x => x.Id).Column("FundPerformanceStatistics_FundId");
Map(x => x.CountryId).Column("FundPerformanceStatistics_FundCountryId");
Map(x => x.ConfigId).Column("FundPerformanceStatistics_ConfigId");
Map(x => x.OneMonth).Column("FundPerformanceStatistics_OneMonthBack");
Map(x => x.ThreeMonth).Column("FundPerformanceStatistics_ThreeMonthBack");
Map(x => x.YTD).Column("FundPerformanceStatistics_YearToDate");
}
}
I have a list of Funds
, each of which has an ID
, ConfigID
and a CountryID
. I need to select FundPerformances
for each Fund
, based on these IDs.
The FundPerformance
table has a composite primary key of ID
, ConfigID
& CountryID
.
So far, I have
perfs = _session.QueryOver<FundPerformance>()
.Where(xx => xx.ConfigId == _configId)
.Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.Id), funds.Select(xx => xx.Id).ToArray()))
.Where(Restrictions.In(Projections.Property<FundPerformance>(t => t.CountryId), funds.Select(xx => xx.CountryId).ToArray()))
.List<FundPerformance>().ToList();
I don't think this is going to work because it's selecting on the individual fields, and not the composite ID.
What do I need to do to select the data properly?
This is the current FundPerformance mapping class:
public class FundPerformanceMap : ClassMap<FundPerformance>
{
public FundPerformanceMap()
{
Id(x => x.Id).Column("FundPerformanceStatistics_FundId");
Map(x => x.CountryId).Column("FundPerformanceStatistics_FundCountryId");
Map(x => x.ConfigId).Column("FundPerformanceStatistics_ConfigId");
Map(x => x.OneMonth).Column("FundPerformanceStatistics_OneMonthBack");
Map(x => x.ThreeMonth).Column("FundPerformanceStatistics_ThreeMonthBack");
Map(x => x.YTD).Column("FundPerformanceStatistics_YearToDate");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您正在寻找其他东西
原始答案:
如果 Fund 有一个 compositeKey,那么 FundPerformance 也应该有一个。另外,我认为对基金的引用更像是面向对象的:
要么将其映射为组件(因为它看起来像依赖的东西)
,要么映射为具有复合键的实体
Maybe you are looking for something else
Original answer:
if Fund has a compositeKey then FundPerformance should have one, too. Also i think a Reference to Fund is more OOP-like:
Either map it as Component (because it looks like something dependant)
or as an Entity with composite key