我有一个ado.net语句,我想转换为使用NHibernate:
Dim sql As New StringBuilder()
sql.AppendLine("SELECT r.RoleId, r.RoleName ")
sql.AppendLine("FROM dbo.aspnet_Roles r ")
sql.AppendLine("WHERE r.RoleId IN ")
sql.AppendLine(" (select roleID from dbo.MenuRole where menuId = @MenuId) ")
sql.AppendLine("Order By r.RoleName")
稍后,我用以下参数填充参数: cmd.Parameters.AddWithValue(“@ MenuId”,menuId)
考虑到我想要返回:IList(Of AspnetRole)
我正在使用:
Dim managerFactory As IManagerFactory = New ManagerFactory()
Dim roleManager As IAspnetRoleManager = managerFactory.GetAspnetRoleManager()
如何使用nHiberate构建和使用该查询?
(P.S。我正在使用Codesmithtools和VB.net以及VS2008和SQL Server 2008)
答案 0 :(得分:1)
首先,您必须将aspnet_roles表和MenuRole表映射到相应的类。一旦你映射了它们,我还会将多对一的MenuRole属性映射到AspnetRole类。
完成后,条件查询应如下所示:
Dim c As ICriteria = Session.CreateCriteria(TypeOf(AspnetRole))
c.Add(Restrictions.Eq("Menu.Id", menuId)
return c.List(Of AspnetRole)