/*
*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:
| 对此文章发表了评论 |

