惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MongoDB | Blog
MongoDB | Blog
AI
AI
B
Blog RSS Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threatpost
I
Intezer
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threat Research - Cisco Blogs
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
Schneier on Security
Webroot Blog
Webroot Blog
Recorded Future
Recorded Future
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
Simon Willison's Weblog
Simon Willison's Weblog
MyScale Blog
MyScale Blog
Project Zero
Project Zero
L
LangChain Blog
B
Blog
D
DataBreaches.Net
Microsoft Security Blog
Microsoft Security Blog
F
Fortinet All Blogs
美团技术团队
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
D
Docker
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
S
Security Affairs
Attack and Defense Labs
Attack and Defense Labs
N
News | PayPal Newsroom
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
V2EX - 技术
V2EX - 技术
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
P
Proofpoint News Feed
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
The Hacker News
The Hacker News
G
GRAHAM CLULEY

博客园 - andy.wu

聊聊除了生产率之外的东西,比如赚钱效率 一次思维锻炼,使用拼音模糊匹配中文 - andy.wu - 博客园 一些需要解决的问题(Win32) svn howto: svn合并时报错:retrieval of mergeinfo unsupported by 。 Windows Mobile应用开发(1):使用Win32 SDK 开发屏幕手电程序 vs(2005 and 2008)中使用vc++创建智能设备项目失败的正确解决方案 思考:google的opensocial的实现原理 思考:日期类型的数据应该用什么样的具体形式存储到数据库? google got crazy!!! EnableViewState详细分析 - andy.wu - 博客园 AjaxPro基础知识 and FAQ 在iis 6中使用共享目录作为虚拟目录 初学wpf感想 Sql Server 2005全文检索中碰到的问题和分析 asp.net faq: 在html文件中,用js获取session? 给博客园首页管理的建议 china-pub,当当,卓越购书经验谈 使用Emacs代替Windows下的Command shell 经验:使用.net 2.0中的TransactionScope碰到的问题
NHibernate Tips: 要注意模型与数据库在Null方面的匹配
andy.wu · 2008-11-07 · via 博客园 - andy.wu

先看一下说明代码,很简单,只为了说明情况

[ActiveRecord(Lazy=true)]
public class MyUser
{
    private int _id;
    [PrimaryKey(Generator=PrimaryKeyType.Identity)]
    public virtual int Id
    {
        get { return _id; }
        set { _id = value; }
    }

    private string _loginId;
    [Property]
    public virtual string LoginId
    {
        get { return _loginId; }
        set { _loginId = value; }
    }

    private bool _isLock = false;
    [Property(SqlType="int")]
    public virtual bool IsLock
    {
        get { return _isLock; }
        set { _isLock = value; }
    }

    private DateTime _createDate = DateTime.Now;
    [Property]
    public virtual DateTime CreateDate
    {
        get { return _createDate; }
        set { _createDate = value; }
    }
}

当使用如下代码时
string hql = " from MyUser ";
session.CreateQuery(hql).List();    // 第1次
session.CreateQuery(hql).List();    // 第2次

在第2次时,会报错:
SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。

经调试发现(费时1个多小时,汗),结果发现只是因为数据库里CreateDate字段为空,导致该错误。将CreateDate字段设值后,不会报错了,但观察输出的sql语句,发现在第2次之前,NH产生了一个update语句,又经调试发现,是由于数据库中IsLock字段为空。将IsLock字段设值后,世界清静了。

总结
使用NHibernate时,要注意模型与数据库在Null方面的匹配。如果数据库允许为空,则模型的类型也应该是可为空的类型,比如应当使用DataTime?对应数据库中可为空的日期字段。.Net 1.1中没有可为空的DateTime类型,那只能在数据库中都设置为Not Null即可。

ps: 好象很多人都认为数据库设计中,最好不要让字段可为空。以前并不在意,但看来NH是坚定的执行了这一准则了