我们经常会遇到某个字段可以为空的情况,以下面的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)));
}
}
// 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;
}
}
[Property()]
public System.DateTime ? Birthday
{
get
{
return this ._birthday;
}
set
{
this ._birthday = value;
}
}
以上代码告诉框架Birthday字段可以为空值,这样框架就能够正确的去处理。当然还可以采用其他的方法比如:使用框架自带的空属性。我个人还是倾向于第一种方法,比较简单。