我有一个NHibernate
Linq查询,它没有按我的预期工作.
问题似乎来自于在where子句中使用左连接表中的可空int列.这导致联接像内部联接一样运行.
var list = this.WorkflowDiaryManager.WorkflowActionRepository.All .Fetch(x => x.CaseView) .Fetch(x => x.WorkflowActionType) .ThenFetchMany(x => x.WorkflowActionPriorityList) .Where(x => x.AssignedUser.Id == userId || x.CaseView.MooseUserId == userId)
由此产生的sql看起来像(从加入开始 – 您不需要查看所有选择)
from Kctc.WorkflowAction workflowac0_ left outer join Kctc.WorkflowCaseView workflowca1_ on workflowac0_.CaseId=workflowca1_.CaseId left outer join Kctc.WorkflowActionType workflowac2_ on workflowac0_.WorkflowActionTypeId=workflowac2_.WorkflowActionTypeId left outer join Kctc.WorkflowActionPriority workflowac3_ on workflowac2_.WorkflowActionTypeId=workflowac3_.WorkflowActionTypeId,Kctc.WorkflowCaseView workflowca4_ where workflowac0_.CaseId=workflowca4_.CaseId and (workflowac0_.AssignedUser=@p0 or workflowca4_.[MooseUserId]=@p1); @p0 = 1087 [Type: Int32 (0)],@p1 = 1087 [Type: Int32 (0)]
因此导致问题的部分是上面代码片段的第5行.正如您所看到的,NHibernate正在尝试在WorkflowCaseView View上进行“老派”连接.这会导致查询排除WorkflowAction表中没有CaseId的其他有效操作.
任何人都可以解释为什么NHibernate正在编写这个sql,以及我如何鼓励它产生更好的查询?
谢谢!
WorkflowActionMap中的重要位
Table("Kctc.WorkflowAction"); Id(x => x.Id).GeneratedBy.Identity().Column("WorkflowActionId"); References(x => x.WorkflowActionType).Column("WorkflowActionTypeId").Unique(); References(x => x.CompletedBy).Column("CompletedBy"); References(x => x.CaseView).Column("CaseId").Not.Update().Unique(); References(x => x.AssignedUser).Column("AssignedUser");
WorkflowCaseViewMap的重要部分
Table("Kctc.WorkflowCaseView"); Id(x => x.Id).Column("CaseId"); Map(x => x.MooseUserId).Nullable();
看着这个,我想知道我是否应该让HasMany回到另一个方向……
编辑.似乎没有帮助