[英]How do you do additional filtering on a HasMany mapping in Fluent nHibernate?
In a project I'm working on, I have four entities(amongst a bunch of others), WorkOrder, Crew, CrewAssignment, and Contractor. 在我正在进行的项目中,我有四个实体(在众多其他实体中),WorkOrder,Crew,CrewAssignment和Contractor。 Their relationship is like this:
他们的关系是这样的:
The problem I'm having is setting up the last part where a WorkOrder can have multiple CrewAssignments. 我遇到的问题是设置WorkOrder可以有多个CrewAssignments的最后一部分。 What I want to do is ensure that the WorkOrder.CrewAssignments property only returns the CrewAssignments that have a Crew with the same Contractor as the WorkOrder.
我想要做的是确保WorkOrder.CrewAssignments属性仅返回CrewAssignments,其Crew与WorkOrder具有相同的Contractor。 Or, less wordy, "where WorkOrder.Contractor == CrewAssignment.Crew.Contractor".
或者,不那么罗嗦,“在哪里WorkOrder.Contractor == CrewAssignment.Crew.Contractor”。
The only thing I've been able to come up with is this, but it throws an exception about the x variable being undefined. 我唯一能想到的就是这个,但它抛出了一个关于x变量未定义的异常。
HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").Where(x => x.Crew.Contractor == x.WorkOrder.Contractor);
Is doing something like this even possible? 做这样的事情甚至可能吗? Or am I barking up the wrong tree entirely?
还是我完全吠叫了错误的树? Google's been failing me all morning with this one.
谷歌整个上午一直没有让我失望。 Any ideas?
有任何想法吗?
I don't know if it could help you but here is my take on in. 我不知道它是否可以帮助你,但这是我接受的。
In FluentNHibernate you have a method called ApplyFilter
that you can set up in your mappings in order to filter on child collections : 在FluentNHibernate中,您有一个名为
ApplyFilter
的方法,您可以在映射中设置该方法以过滤子集合:
HasMany(x => x.CrewAssignments).KeyColumn("WorkOrderID").ApplyFilter<MyFilter>().Cascade.AllDeleteOrphan();
Then you can create your filter : 然后你可以创建你的过滤器:
public class MyFilter: FilterDefinition
{
public MyFilter()
{
WithName("contractor").WithCondition("Crew.ContractorId== :contractorId").AddParameter("contractorId", NHibernate.NHibernateUtil.Int32);
}
}
Then in the implementation of your repository, you invoke the filter : 然后在您的存储库的实现中,您调用过滤器:
Sessions.EnableFilter("contractor").SetParameter("contractorId", 1254);
I don't know if this is the best solution but the only one that comes to my mind right now. 我不知道这是不是最好的解决方案,而是我现在唯一想到的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.