行情北京 上海 广州 深圳 沈阳 济南 郑州 武汉 长沙 南京 西安 成都 昆明 杭州
e杂志下载
 首页 | 资讯 网刊 视频 评测  企业:服务器 网络 存储 通信 安全 技术开发 信息化-方案 | ITPUB IXPUB
 商务笔记本 台式机 投影机 打印扫描 办公产品 耗材 软件 学院 下载 驱动  家庭数字家电 家庭组网
 个人DIY硬件 | 手机 GSM CDMA 无线电 GPS| 数码影像 相机 摄像机 | 消费数码 MP3 | 论坛 | 经销商社区
 报价中心 三维图秀 产品评论 产品大全 使用手册 术语详解 厂商专区 二手市场 维修服务 疑难解答 IT搜索
 

NHibernate文档-Nullables

作者:DDLLY命名空间 发表日期:2007-04-26 02:02
  内容导航: 上一页 1 下一页
 
[IT168 技术文档]第11章 Nullables

目录

如何使用?

什么是 Nullables?

Nullables 是 NHibernate 的附加软件,它是Donald L Mull Jr. (aka luggage)贡献的.大部分数据库系统允许基本类型(象intbool)为null。这意味着一个boolean列可能有0,1或者是null值,null和0有不同的含义。但是在.NET 1.x这是不能实现的;一个bool不是true就是false。

Nullables使得在NHibernate中使用nullable的基本类型成为可能。注意,.NET 2.0已经有了这个特性。

如何使用?

这里是一个简单的例子,它使用了Nullables.NullableDateTime来(可选择的)保存一个人(Person)的生日。

public class Person { int _id; string _name; Nullables.NullableDateTime _dateOfBirth; public Person() { } public int Id { get { return this._id; } } public string Name { get { return this._name; } set { this._name = value; } } public Nullables.NullableDateTime DateOfBirth { get { return this._dateOfBirth; } set { this._dateOfBirth = value; } } }

如你所见,DateOfBirth是Nullables.NullableDateTime类型(而不是System.DateTime)。

这里是映射

<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"> <class name="Example.Person, Example" table="Person"> <id name="Id" access="field.camelcase-underscore" unsaved-value="0"> <generator class="native" /> </id> <property name="Name" type="String" length="200" /> <property name="DateOfBirth" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" /> </class> </hibernate-mapping>
重要

在这个映射中,DateOfBirth的类型必须Nullables.NHibernate.NullableDateTimeType。注意NHibernate.Mapping.Attributes会自动处理它。

Nullables.NHibernate.NullableXXXType是用来转换Nullables 类型到数据库的包装类。

这里是这个例子的部分代码:

Person per = new Person(); textBox1.Text = per.DateOfBirth.Value.ToString() // will throw an exception when there is no value. textBox1.Text = per.DateOfBirth.ToString() // will work. it will return an empty string if there is no value. textBox1.Text = (per.DateOfBirth.HasValue ? per.DateOfBirth.Value.ToShortDateString() : "Unknown") // friendly message per.DateOfBirth = new System.DateTime(1979, 11, 8); // implicit cast from the "plain" System.DateTime. per.DateOfBirth = new NullableDateTime(new System.DateTime(1979, 11, 8)); // the long way. per.DateOfBirth = null; // this works. per.DateOfBirth = NullableDateTime.Default; // this is more correct.
上一页 1 下一页
【内容导航】  
【相关文章】  
下一篇:使用NHibernate.Mapping.Attributes
©版权所有。未经许可,不得转载。 【责任编辑:孙蓬阳
 
  网友评论