Castle ActiveRecord 中空值处理

 我们经常会遇到某个字段可以为空的情况,以下面的Users这个类为例,Birthday这个字段是允许为空值的。

 

//  
//  Generated by ActiveRecord Generator
//  
//
using  Castle.ActiveRecord;


[ActiveRecord(
" Users " )]
public   class  Users : ActiveRecordBase
{
    
    
private   int  _logonID;
    
    
private   string  _logonName;
    
    
private   string  _password;
    
    
private   string  _emailAddress;
    
    
private  System.DateTime _lastLogon;
    
    
private   bool  _available;

    
private  System.DateTime  _birthday;
    [PrimaryKey(PrimaryKeyType.Native)]
    
public   int  LogonID
    {
        
get
        {
            
return   this ._logonID;
        }
        
set
        {
            
this ._logonID  =  value;
        }
    }
    
    [Property()]
    
public   string  LogonName
    {
        
get
        {
            
return   this ._logonName;
        }
        
set
        {
            
this ._logonName  =  value;
        }
    }
    
    [Property()]
    
public   string  Password
    {
        
get
        {
            
return   this ._password;
        }
        
set
        {
            
this ._password  =  value;
        }
    }
    
    [Property(NotNull
= true )]
    
public   string  EmailAddress
    {
        
get
        {
            
return   this ._emailAddress;
        }
        
set
        {
            
this ._emailAddress  =  value;
        }
    }

    [Property(Insert 
=   false )]
    
public  System.DateTime LastLogon
    {
        
get
        {
            
return   this ._lastLogon;
        }
        
set
        {
            
this ._lastLogon  =  value;
        }
    }
    
    [Property( Insert
= false )]
    
public   bool  Available
    {
        
get
        {
            
return   this ._available;
        }
        
set
        {
            
this ._available  =  value;
        }
    }
    [Property()]
    
public  System.DateTime  Birthday
    {
        
get
        {
            
return   this ._birthday;
        }
        
set
        {
            
this ._birthday  =  value;
        }
    }

    
public   static   void  DeleteAll()
    {
        ActiveRecordBase.DeleteAll(
typeof (Users));
    }
    
    
public   static  Users[] FindAll()
    {
        
return  ((Users[])(ActiveRecordBase.FindAll( typeof (Users))));
    }
    
    
public   static  Users Find( int  LogonID)
    {
        
return  ((Users)(ActiveRecordBase.FindByPrimaryKey( typeof (Users), LogonID)));
    }
}

如果不对以上代码做些处理,则会出现以下错误:

SqlDateTime 溢出。必须介于  1 / 1 / 1753   12 : 00 : 00  AM 和  12 / 31 / 9999   11 : 59 : 59  PM 之间。 

解决方案如下:

private  System.DateTime ?  _birthday;
[Property()]
    
public  System.DateTime ?  Birthday
    {
        
get
        {
            
return   this ._birthday;
        }
        
set
        {
            
this ._birthday  =  value;
        }
    }

以上代码告诉框架Birthday字段可以为空值,这样框架就能够正确的去处理。当然还可以采用其他的方法比如:使用框架自带的空属性。我个人还是倾向于第一种方法,比较简单。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值