使用nhibernate在linq中选择查询

时间:2013-12-19 13:04:28

标签: linq nhibernate linq-to-nhibernate

我使用NHibernate 3.3和linq。当我写一个选择查询时,它什么也没有结果。 配置和操作会话的代码很简单。

Dim cfg As New Configuration()
                cfg.Properties.Add(NHibernate.Cfg.Environment. _
                  ConnectionProvider, GetType(NHibernate.Connection. _
                  DriverConnectionProvider).AssemblyQualifiedName)

                cfg.Properties.Add(NHibernate.Cfg.Environment.Dialect, _
                  GetType(NHibernate.Dialect.MsSql2012Dialect). _
                  AssemblyQualifiedName)

                cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionDriver, _
                  GetType(NHibernate.Driver.SqlClientDriver). _
                  AssemblyQualifiedName)

                cfg.Properties.Add(NHibernate.Cfg.Environment.ConnectionStringName, _
  "connectionstring")


                cfg.Properties.Add(NHibernate.Cfg.Environment. _
              ProxyFactoryFactoryClass, GetType _
              (NHibernate.Bytecode.DefaultProxyFactoryFactory). _
              AssemblyQualifiedName)

                Dim s2 As NHibernate.ISessionFactory = cfg.BuildSessionFactory

                db = s2.OpenSession()

到这里它工作正常,查询是

Dim user As myClass = New myClass
            Dim query = (From my_table In db.Query(Of myClass)() Where user.fname = name Select IMSDK_Users).SingleOrDefault

但查询没有返回任何内容。 请帮忙谢谢

1 个答案:

答案 0 :(得分:0)

包含这些名称空间:

Imports System.Linq
Imports NHibernate
Imports NHibernate.Linq

试试这个:

Dim query = (From c In db.Query(Of myClass)() Where c.fname = name Select c).FirstOrDefault()