zoukankan      html  css  js  c++  java
  • C#.NET使用NHibernate 1.0 XML映射使用中容易出错的地方全程记录(转)

    /*
    *Description:C#.NET使用NHibernate 1.0 XML映射使用中容易出错的地方全程记录
    *Auther:天很蓝_崇崇
    *MSN:chongchong2008@msn.com
    *Dates:2005-12-14
    *Copyright:ChongChong2008 YiChang HuBei China
    */

    1 数据库中是bit类型的数据 hbm.xml映射文件中的类型可以为Int32 或是 boolean
     关于数据库中是bit类型的数据 hbm.xml映射文件中的类型可以为Int32
     千万不要写成int,那是错误的。
     也可以为boolean或是Boolean,但是别要别写成bool,否则也会出错的!!

    2 实体数据定义里的类型要和XML映射文件中定义的类型一致

     public string IP //只读属性
     {
      get /* 注意注释掉可写属性,绑定此数据的时候会报错 */
      {
       this.ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() ;
       return this.ip ;
      }
     }
    在这里确定好类型以后,在实体数据定义里的类型要和XML映射文件中定义的类型一致,
    大家看下面的定义就不一致
     XML映射文件
     <property name="IsPass" type="boolean" length="1"/>
     数据实体文件
     public int IsPass
     {
      get{ return this.isPass ; }
      set{ this.isPass = value ; }
     }
    如果你像上面那样写的画,会报错如下
    Server Error in '/MyNH' Application.
    --------------------------------------------------------------------------------
    Specified cast is not valid.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.InvalidCastException: Specified cast is not valid.
    Source Error:

    Line 68:     try
    Line 69:     {
    Line 70:      session.Save( userEntity );//保存数据实体
    Line 71:      session.Flush() ;
    Line 72:
         transaction.Commit() ; //提交事务
    正确的应该写成这样 :
     XML映射文件
     <property name="IsPass" type="boolean" length="1"/>
     数据实体文件
     public bool IsPass
     {
      get{ return this.isPass ; }
      set{ this.isPass = value ; }
     }

      3 应该将可读写将属性绑定到服务器控件上

      在你把NH返回的IList对象绑定到DataGrid等服务器控件的时候要注意,如果你得服务器控件使用了一个属性。

      比如说是IP , 虽然这个属性完全可以定义为只读的 , 但是如果你需要把这个属性绑定到DataGrid等服务器控件的某一个列的话,

     那么就请你需要特别的注意了.

     你需要把这个只读属性定义成一个可以读些的属性, 虽然定义为可写的对你来说没有任何意义 ,
            但是NH需要写属性才可以帮定到正确的数据,
            否则,会报如下的错误:
     如下代码,DataGrid使用(绑定)了IP这个属性
      public string IP //只读属性
      {
       get
       {
         this.ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() ;
         return this.ip ;
       }
      }

    Server Error in '/MyNH' Application.
    --------------------------------------------------------------------------------
    Property set method not found.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.ArgumentException: Property set method not found.
    Source Error:

    Line 53:    using( ISession session = NHelper.Sessions().OpenSession() ) //获取会话工厂的当前会话
    Line 54:    {
    Line 55:     return session.Find( "from CHONGCHONG.MyNH.Business.UserEntity" ) ;
    Line 56:    }
    Line 57:   }
     
    Source File: d:\chongchong\chongchong_web\mynh\business\businesslogic\businesslogic.cs    Line: 55
    Stack Trace:

     正确的代码应该这样 :
      public string IP
      {
       get
       {
         this.ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString() ;
         return this.ip ;
       }
         
       set /* 注意注释掉可写属性,绑定此数据的时候会报错 */
       {
        this.ip = value ;
       }
      }

     另外还要注意一点, 如果你返回的IList集合为空的话,直接将返回的数据绑定到你得控件上,也会报错!!

  • 相关阅读:
    C# httpclient获取cookies实现模拟web登录
    C# httpclient获取cookies实现模拟web登录
    长连接与短连接的区别以及使用场景
    长连接与短连接的区别以及使用场景
    vuejs项目性能优化总结
    vuejs项目性能优化总结
    C# 发送HTTP请求(可加入Cookies)
    C# 发送HTTP请求(可加入Cookies)
    集合框架系列教材 (十六)- 其他
    集合框架系列教材 (十五)- 关系与区别
  • 原文地址:https://www.cnblogs.com/kedach/p/1283121.html
Copyright © 2011-2022 走看看