NHibernate ICriteria返回null

时间:2014-12-25 21:42:11

标签: c# wcf nhibernate

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

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

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

<?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: 到目前为止,这是本课程中唯一的方法:

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();
        }
    }
}

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

enter image description here

我有什么遗漏的吗?我的映射文件是否正确?我非常感谢你们的一些意见。

编辑:查看VS2012中的输出选项卡,我发现此消息:mscorlib.dll中出现'System.IO.DirectoryNotFoundException'类型的第一次机会异常

EDIT2(2014年12月27日,17.51)

  

无法执行查询[SELECT this_.ccode as ccode2_0_,this_.cname as cname2_0_,this_.credit as credit2_0_ FROM Courses this_] [SQL:SELECT this_.ccode as ccode2_0_,this_.cname as cname2_0_,this_.credit as credit2_0_ FROM Courses this_]服务器堆栈跟踪:vid System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息回复,MessageFault错误,字符串操作,MessageVersion版本,FaultConverter faultConverter)vid System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&amp; ; rpc)vid System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)vid System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)vid System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)在[0]处重新抛出异常:vid System.Runtime.Remoting.Proxies.RealProx y.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)vid System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp; msgData,Int32 type)vid IServiceManager.RetrieveData()vid ServiceManagerClient.RetrieveData()

1 个答案:

答案 0 :(得分:0)

根据事实,这句话: foreach (Course course in cList) 无效,即cList empty - 我'd怀疑错误的映射文件。嗯......

1)有人认为,rsultMngSystmMapper.hbm.xml未正确标记为 Embedded Resource 。检查该文件的属性及其 Build Action

2)映射文件位于不同的库中,然后是POCO对象。设置显示:<mapping assembly="RManageSystemService"/>并且因为POCO在此库中肯定存在,所以可能是您的映射文件位于其他位置,例如的 RManageSystemService.Data

映射文件的这些“错误”设置之一可能是罪魁祸首。

  

如果我们要求 NOT 映射的实体/对象的标准,NHibernate确实表现得有些奇怪。它只是在结尾处返回空列表,没有任何消息或异常......