Nhibernate - 坚持分离标准(asp.net mvc 1 和 nhibernate 2)c#
好的,所以我找不到一个很好的例子,这样我就可以更好地理解如何使用独立的标准(假设这就是我首先想要使用的)。
我有 2 张桌子。 Placement 和 PlacementSupervisor
我的 PlacementSupervisor 表有一个 PlacementID 的 FK,它与 Placement.PlacementID 相关 - 尽管我的 nhibernate 模型类有 PlacementSupervisor 。展示位置(而不是专门指定展示位置 ID 的属性 - 不确定这是否重要)。
我想做的是 - 如果传递了主管 ID 的值,我想限制该主管 ID 的展示位置。
已经尝试过:
ICriteria query = m_PlacementRepository.QueryAlias("p")
....
if (criteria.SupervisorId > 0 && !string.IsNullOrEmpty(criteria.SupervisorTypeId))
{
DetachedCriteria entityQuery = DetachedCriteria.For<PlacementSupervisor>("sup")
.Add(Restrictions.And(
Restrictions.Eq("sup.supervisorId", criteria.SupervisorId),
Restrictions.Eq("sup.supervisorTypeId", criteria.SupervisorTypeId)
))
.SetProjection(Projections.ProjectionList()
.AddPropertyAlias("Placement.PlacementId", "PlacementId")
);
query.Add(Subqueries.PropertyIn("p.PlacementId", entityQuery));
}
这只是给了我错误: 无法找到匹配的标准信息提供程序:(sup.supervisorId = 5 和sup.supervisorTypeId = U)
首先,supervisorTypeId 是一个字符串。 其次,我不明白如何实现我想要做的事情,所以只是尝试了投影、属性别名和子查询选项的各种组合......因为我不知道应该如何加入到另一个表/entity 当 FK 键位于第二个表中时。
有人能指出我正确的方向吗?从数据的角度来看,这似乎是一件很容易做的事情,希望我只是错过了一些明显的东西!
OK so I can't find a good example of this so I can better understand how to use detached criteria (assuming that's what I want to use in the first place).
I have 2 tables.
Placement and PlacementSupervisor
My PlacementSupervisor table has a FK of PlacementID which relates to Placement.PlacementID - though my nhibernate model class has PlacementSupervisor . Placement (rather than specifically specifying a property of placement ID - not sure if this is important).
What I am trying to do is - if values are passed through for the supervisor ID I want to restrict placements with that supervisor id.
Have tried:
ICriteria query = m_PlacementRepository.QueryAlias("p")
....
if (criteria.SupervisorId > 0 && !string.IsNullOrEmpty(criteria.SupervisorTypeId))
{
DetachedCriteria entityQuery = DetachedCriteria.For<PlacementSupervisor>("sup")
.Add(Restrictions.And(
Restrictions.Eq("sup.supervisorId", criteria.SupervisorId),
Restrictions.Eq("sup.supervisorTypeId", criteria.SupervisorTypeId)
))
.SetProjection(Projections.ProjectionList()
.AddPropertyAlias("Placement.PlacementId", "PlacementId")
);
query.Add(Subqueries.PropertyIn("p.PlacementId", entityQuery));
}
Which just gives me the error:
Could not find a matching criteria info provider to: (sup.supervisorId = 5 and sup.supervisorTypeId = U)
Firstly supervisorTypeId is a string.
Secondly I don't understand how to achieve what I'm trying to do so have just been trying various combinations of projections, and property aliases and subquery options..as I don't get how I'm supposed to join to another table/entity when the FK key sits in the second table.
Can someone point me in the right direction. It seems like such an easy thing to do from a data perspective that hopefully I'm just missing something obvious!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会有所帮助。这是 Fabio Maulo 对标准 api 的一个很好的概述
一般来说,如果您不想立即在会话中使用 DeteachedCriterias,那么您会使用它,所以听起来您并不真正需要它。
上面的链接(第 13.4 节)给出了一个示例(我已对其进行了修改以适合您的条款):
其他一些注意事项:
This might help. It's a good overview of the criteria api by Fabio Maulo
Generally you use DeteachedCriterias if you don't want to immediately use it in a session, so it doesn't sound like you really need one.
The above link (section 13.4) gives an example (which I've modified to fit your terms):
A few other notes:
我最终能够使用上面修改后的代码。
主管是我的安置模型类的一个属性。
另请注意,限制已半弃用表达式。
I ended up being able to use the code from above modified.
With Supervisors being a property on my Placement model class.
Note also that Expressions has been semi-deprecated by Restrictions.