首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NHibernate ICriteria返回null

NHibernate ICriteria返回null
EN

Stack Overflow用户
提问于 2014-12-25 21:42:11
回答 1查看 843关注 0票数 1

我正在尝试创建一个连接到数据库的wcf服务。对于连接,我使用NHibernate。以下是也映射到文件rsultMngSystmMapper.hbm.xml中的类课程

代码语言:javascript
复制
[DataContract]
public class Course
{
    private int ccode;
    private string cname;
    private int credits;

    [DataMember]
    public int CCode
    {
        get
        {
            return ccode;
        }
        set
        {
            ccode = value;
        }
    }
   etc...
}

上述类中的每个属性(带有getter和setter)都具有属性DataMember。现在文件rsultMngSystmMapper.hbm.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="RManageSystemService"
                   namespace="RManageSystemService.orm_rman_systm">
  <class name="Course" table="Courses" lazy="false">
    <id name="CCode" column="ccode">
      <generator class="identity"/>
    </id>
    <property name="CName" column="cname"/>
    <property name="Credits" column="credit"/>
  </class>
</hibernate-mapping>

现在文件hibernate.cfg.xml:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=Mnemonics;User ID=Mnmncs;Password=mnmncs;Initial Catalog=database-name;Integrated Security=true</property>
    <property name="show_sql">true</property>
    <mapping assembly="RManageSystemService"/>
  </session-factory>
</hibernate-configuration>

我应该把上面的代码放在web.config中吗?或者把它分开可以吗?

现在是类RMngrDataManipulation:到目前为止,这是该类中的唯一方法:

代码语言:javascript
复制
public static Course RetrieveData(Course c)
{
    Configuration myConfig;
    ISessionFactory mySessFac;
    ISession mySess;
    myConfig = new Configuration();
    myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
    mySessFac = myConfig.BuildSessionFactory();
    mySess = mySessFac.OpenSession();
    try
    {
        ICriteria criteria = mySess.CreateCriteria<Course>();
        IList<Course> cList = criteria.List<Course>();
        foreach (Course course in cList)
        {
            c = course;
        }
        //This one is empty
        return c;
    }
    catch (Exception e)
    {
        throw;
    }
    finally
    {
        if (!mySessFac.IsClosed)
        {
            mySessFac.Close();
        }
    }
}

在中从类RetrieveData()调用方法CourseService.svc.cs时得到的结果如下

我错过了什么吗?我的映射文件正确吗?我非常感谢你们的一些投入。

编辑:查看VS2012中的output选项卡,我发现了以下消息:在mscorlib.dll中出现了第一次出现“System.IO.DirectoryNotFoundException”类型的异常

EDIT2(27.12.2014,17.51)

无法执行查询选择this_.ccode作为ccode2__、this_.cname作为cname2__、this_.credit作为credit2__从this_ Server堆栈跟踪: vid System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage应答、MessageFault故障、字符串操作、MessageVersion版本、FaultConverter faultConverter) vid System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作、ProxyRpc& rpc)、vid System.ServiceModel.Channels.ServiceChannel.Call(String操作、布尔单向操作、ProxyOperationRuntime操作、Object[] ins、faultConverter outs、faultConverter超时) vid System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage,( vid System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)异常重新引发: vid System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg) vid System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32类型) vid IServiceManager.RetrieveData() vid ServiceManagerClient.RetrieveData()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-26 14:31:45

基于这样的事实,这个声明:foreach (Course course in cList)什么也不做,即cListempty --我怀疑是错误的映射文件。好吧..。

( 1)人们可能认为,Embedded Resource.没有正确地标记rsultMngSystmMapper.hbm.xml检查该文件及其Build Action的属性

2)映射文件与POCO对象在不同的库中。设置是这样的:因为POCO在这个库中是肯定的,所以您的映射文件可能在其他文件中,例如RManageSystemService.Data

其中一个映射文件的“错误”设置可能是罪魁祸首。

如果我们要求为NHibernate (而不是映射的实体/对象)提供一个条件,那么的行为确实有点奇怪。它只是在结尾返回空列表,没有任何消息或异常.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27650940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档