0

我在尝试将数据加载到 ASP.NET 页面中的 GridView 时遇到问题。我对 NHibernate 很陌生,现在尝试将其用作映射工具。

我有以下 XML 映射模式:

地位

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="CMMS.BLL.Status, CMMS" table="tblStatus">
    <id name="StatusID" column="StatusID" unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="StatusCode" column="StatusCode" />
    <property name="StatusNote" column="StatusNote" />
  </class>
</hibernate-mapping>

工作指示

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="CMMS.BLL.WorkOrder, CMMS" table="WorkOrder">
        <id name="ID" column="ID" type="Int32"> 
            <generator class="identity" /> 
        </id>
        <property name="WOID" column="WOID" type="String" length="50" />
        <property name="WOReference" column="WOReference" type="String" length="50" />      

            <set name="WorkOrderStatus" table="tblWorkOrderStatus" inverse="true" cascade="all-delete-orphan">
                <key column="WorkOrderID" />
                <one-to-many class="CMMS.BLL.WorkOrderStatus, CMMS" />
         </set> 
    </class>
</hibernate-mapping>

工单状态

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="CMMS.BLL.WorkOrderStatus, CMMS" table="tblWorkOrderStatus">
    <id name="WSID" column="WSID" type="Int32" unsaved-value="0">
      <generator class="identity" />
    </id>

    <property name="Comments" column="Comments" />
    <property name="LastModifiedOn" column="LastModifiedOn"  type="Timestamp" />
    <property name="CreatedBy" column="CreatedBy" />

    <many-to-one name="WorkOrder" class="CMMS.BLL.WorkOrder, CMMS" column="WorkOrderID" />
    <many-to-one name="Status" class="CMMS.BLL.Status, CMMS" column="StatusID" />
  </class>
</hibernate-mapping>

及其 POCO 类定义如下:

地位

public class Status {
    /* Private parameter of object*/
    private int _statusid;
    private string _statuscode ;
    private string _statusnote ;
    //private ISet<WorkOrderStatus> _workorder_status = new HashedSet<WorkOrderStatus>();

    /* Public methode to access object*/
    public virtual int StatusID{
        get { return this._statusid; }
        set { this._statusid  = value; }
    }        

    public virtual string StatusCode{
        get { return _statuscode ; }
        set { _statuscode  = value; }
    }

    public virtual string StatusNote{
        get { return _statusnote; }
        set { _statusnote = value; }
    }

    /*
    public virtual ISet<WorkOrderStatus> WorkOrderStatus
    {
        get { return (_workorder_status); }
        protected set { _workorder_status = value; }
    }
     */ 

    /* Class Constructor */
    public Status() { }
}

工作指示

public class WorkOrder
{
    private int _ID;
    private string _WOID;
    private string _WOReference;        
              private ISet<WorkOrderStatus> _workorder_status;


    public virtual int ID
    {
        get { return this._ID; }
        set { this._ID = value; }
    }

    public virtual string WOID
    {
        get { return this._WOID; }
        set { this._WOID = value; }
    }

         public virtual string WOReference
    {
        get { return _WOReference; }
        set { _WOReference = value; }
    }

    public virtual ISet<WorkOrderStatus> WorkOrderStatus{
                get { return (_workorder_status); }
                protected set { _workorder_status = value; }
     }

    public WorkOrder(){ 
                this._workorder_status = new HashedSet<WorkOrderStatus>();
        } 
}

工单状态

public class WorkOrderStatus 
 {

     private int _wsid;
     private string _comments;        
     private DateTime _lastmodifiedon;
     private WorkOrder _workorder;
     private Status _status;
     private int _createdby;


     public virtual int WSID {
         get { return this._wsid; }
         set { this._wsid = value; }
     }

     public virtual string Comments {
         get { return this._comments; }
         set { this._comments = value; }
     }

     public virtual DateTime LastModifiedOn{ 
         get { return _lastmodifiedon; }
         set { _lastmodifiedon = value; }
     }

     public virtual  WorkOrder WorkOrder {
         get { return _workorder; }
         set { _workorder = value; }
     }
     public virtual Status  Status {
         get { return _status; }
         set { _status = value; }
     }

     public virtual int CreatedBy {
         get { return _createdby; }
         set { _createdby = value; }
     }


     public WorkOrderStatus() { }
 }

但是,当我将 GridView 绑定到 ObjectDataSource 到执行以下语句的方法时:

string strQuery = "SELECT wo.*, st.*, ws.* FROM WorkOrder wo " +
                       "INNER JOIN ( " +
                       "             SELECT * " +
                       "             FROM tblWorkOrderStatus o1 " +
                       "             WHERE LastModifiedOn=( " +
                       "                                    SELECT TOP 1 LastModifiedOn " +
                       "                                    FROM tblWorkOrderStatus o2 " +
                       "                                    WHERE (o1.WorkOrderID = o2.WorkOrderID) " +
                       "                                    ORDER BY LastModifiedOn DESC) " +
                       ")ws ON wo.ID= ws.WorkOrderID " +
                       "INNER JOIN tblStatus st ON ws.StatusID= st.StatusID ";


   strQuery += "ORDER BY wo.WOID";


IList  wo = session.CreateSQLQuery(strQuery)
      .AddEntity("wo", typeof(WorkOrder))
                 .AddEntity("st", typeof(Status))
          .AddEntity("ws", typeof(WorkOrderStatus))  
                .List();
return wo;

我对表 tblWorkOrderStatus 进行了一系列更新,而没有在 SQL Server Profiler 上看到我的函数的任何调用。

RPC:Completed   exec sp_executesql N'UPDATE CMMS.dbo.tblWorkOrderStatus SET Comments = @p0, LastModifiedOn = @p1, CreatedBy = @p2, WorkOrderID = @p3, StatusID = @p4 WHERE WSID = @p5',N'@p0 nvarchar(4000),@p1 datetime,@p2 int,@p3 int,@p4 int,@p5 int',@p0=NULL,@p1='2012-07-19 09:44:45.4600000',@p2=0,@p3=7223,@p4=1,@p5=104147    .Net SqlClient Data Provider        
RPC:Completed   exec sp_executesql N'UPDATE CMMS.dbo.tblWorkOrderStatus SET Comments = @p0, LastModifiedOn = @p1, CreatedBy = @p2, WorkOrderID = @p3, StatusID = @p4 WHERE WSID = @p5',N'@p0 nvarchar(4000),@p1 datetime,@p2 int,@p3 int,@p4 int,@p5 int',@p0=NULL,@p1='2012-07-19 09:44:45.4600000',@p2=0,@p3=7226,@p4=1,@p5=104148    .Net SqlClient Data Provider        
RPC:Completed   exec sp_executesql N'UPDATE CMMS.dbo.tblWorkOrderStatus SET Comments = @p0, LastModifiedOn = @p1, CreatedBy = @p2, WorkOrderID = @p3, StatusID = @p4 WHERE WSID = @p5',N'@p0 nvarchar(4000),@p1 datetime,@p2 int,@p3 int,@p4 int,@p5 int',@p0=NULL,@p1='2012-07-19 09:44:45.4600000',@p2=0,@p3=7234,@p4=1,@p5=104150    .Net SqlClient Data Provider        
RPC:Completed   exec sp_executesql N'UPDATE CMMS.dbo.tblWorkOrderStatus SET Comments = @p0, LastModifiedOn = @p1, CreatedBy = @p2, WorkOrderID = @p3, StatusID = @p4 WHERE WSID = @p5',N'@p0 nvarchar(4000),@p1 datetime,@p2 int,@p3 int,@p4 int,@p5 int',@p0=NULL,@p1='2012-07-19 09:44:45.4600000',@p2=0,@p3=7235,@p4=1,@p5=104151    .Net SqlClient Data Provider    

我不知道到底是什么问题。可能是我的映射文件或我的数据库有问题?请你帮助我好吗?我可以做些什么来管理这个解决方案?

4

1 回答 1

1

这种现象最常见的原因似乎是枚举映射为整数,而 null 值映射为不可为空。当空值从数据库返回并映射到一个实例上时,它通常会获得一个默认值 - 然后这被 NHibernate 视为更改,因此当您的会话被刷新时,它会保存任何受影响的实体。

快速查看您的映射,我没有看到任何枚举,所以我怀疑您的 WorkOrderStatus 表中有一个列定义为可能不应该为空的列。我敢打赌它是“CreatedBy”列,因为每次更新都会将该列设置为 0(整数的默认值)。我怀疑 CreatedBy 不应该真的可以为空,因此更新映射/实体以使其成为可能并不理想。您可能希望使列不可为空,并修复当前为空的任何行以具有某种默认值。

有关更多信息,请查看如何测试您的映射:捉鬼敢死队

于 2012-07-30T13:16:40.803 回答