Chapter 2 Nullables

Source: Internet
Author: User
Chapter 2 Nullables

Directory

How to use it?

What is Nullables?

Nullables is an additional software for nhib.pdf. It is Donald L Mull Jr. (akaLuggage) Contribution.Most database systems allow basic types (suchintOrbool) Is null. This means that a boolean column may have 0, 1 or null values. null and 0 have different meanings. However, this cannot be implemented in. NET 1. x. If a bool is neither true nor false.

Nullables makes it possible to use the basic nullable type in NHibernate. Note that. NET 2.0 already has this feature.

How to use it?

Here is a simple example, which usesNullables.NullableDateTimeTo (optional) save a person (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; }    }}

As you can see, DateOfBirth isNullables.NullableDateTimeType (insteadSystem.DateTime).

Here is the ing

<?xml version="1.0" encoding="utf-8" ?> Important

In this ing, the DateOfBirth typeRequiredYesNullables.NHibernate.NullableDateTimeType. Note that NHibernate. Mapping. Attributes will automatically process it.

Nullables.NHibernate.NullableXXXTypeIs the packaging class used to convert the Nullables type to the database.

Here is part of the code for this example:

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 messageper.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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.