Nhibernate - 坚持分离标准(asp.net mvc 1 和 nhibernate 2)c#

发布于 2024-10-19 04:23:22 字数 1511 浏览 5 评论 0原文

好的,所以我找不到一个很好的例子,这样我就可以更好地理解如何使用独立的标准(假设这就是我首先想要使用的)。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2024-10-26 04:23:22

这可能会有所帮助。这是 Fabio Maulo 对标准 api 的一个很好的概述

一般来说,如果您不想立即在会话中使用 DeteachedCriterias,那么您会使用它,所以听起来您并不真正需要它。

上面的链接(第 13.4 节)给出了一个示例(我已对其进行了修改以适合您的条款):

IList placements = sess.CreateCriteria(typeof(Placement))
                 .CreateAlias("PlacementSupervisor", "sup")
                 .Add( Expression.EqProperty("sup.supervisorId", criteria.SupervisorId") )
                 .Add( Expression.EqProperty("sup.supervisorTypeId", criteria.SupervisorTypeId) )
                 .List();

其他一些注意事项:

  1. 我不担心 FK。只要您映射了关系,NH 就能弄清楚如何进行连接。
  2. 我认为属性名称区分大小写,所以也尝试使用“sup.SupervisorId”。
  3. 确保您使用的是属性名称而不是数据库列名称。

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):

IList placements = sess.CreateCriteria(typeof(Placement))
                 .CreateAlias("PlacementSupervisor", "sup")
                 .Add( Expression.EqProperty("sup.supervisorId", criteria.SupervisorId") )
                 .Add( Expression.EqProperty("sup.supervisorTypeId", criteria.SupervisorTypeId) )
                 .List();

A few other notes:

  1. I wouldn't worry about FK's. As long as you've mapped the relationship, NH can figure out how to do the join.
  2. Property names are case sensitive I think so try it with "sup.SupervisorId" as well.
  3. Make sure you're using the property name and not the db column name.
我一向站在原地 2024-10-26 04:23:22

我最终能够使用上面修改后的代码。

  query.CreateCriteria("Supervisors")  
      .Add(Restrictions.Eq("SupervisorId", (int)criteria.SupervisorId))
      .Add(Restrictions.Eq("SupervisorType.SupervisorTypeId", criteria.SupervisorTypeId));

主管是我的安置模型类的一个属性。

另请注意,限制已半弃用表达式。

I ended up being able to use the code from above modified.

  query.CreateCriteria("Supervisors")  
      .Add(Restrictions.Eq("SupervisorId", (int)criteria.SupervisorId))
      .Add(Restrictions.Eq("SupervisorType.SupervisorTypeId", criteria.SupervisorTypeId));

With Supervisors being a property on my Placement model class.

Note also that Expressions has been semi-deprecated by Restrictions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文