persisting enum and bool types with NHibernate

Source: Internet
Author: User
Tags bool datetime integer

Opening

When we use the enum and bool type attributes of the nhibernate, we need to persist more generally, and the database is irrelevant.

The enumeration is persisted as a string of corresponding integers, and NH is persisted as an integer by default.

Boolean persistence defaults depending on the type of database, SQL Server is persisted to bit, but many databases do not have bit types and are typically persisted with char.

You can choose to persist to Y and N, or you can choose to persist to 1 and 0.

1. Enumerating the persistence of an enum

The enumeration is persisted into a string, that is, the integer corresponding to the enumeration is persisted to the database.

Public enum Status
   {
       Yes = 1,
       No = 2
   }
public class product
   {public
       product ()
       {this
           . Createtime = DateTime.Now;
           This. Modifytime = DateTime.Now;
       }
       Public virtual Guid Id {get; set;}
       Public virtual string Name {get; set;}
       Public virtual bool Discontinued {get; set;}
       Public virtual DateTime Createtime {get; set;}
       Public virtual DateTime Modifytime {get; set;}
       Public virtual Category Category {get; set;}
       public virtual int Version {get; set;}
       Public virtual bool Isdelete {get; set;}
       Public virtual status Status {get; set;}
   }

Add the Sql-type property directly to the column, and set the corresponding type for the database column.

<property name= "status" >
     <column name= "status" sql-type= "varchar" default= "1" not-null= "true" > </column>
   </property>

2, the persistence of boolean bool

2.1 Persisted to Y and N

<property name= "Isdelete" type= "NHibernate.Type.YesNoType, NHibernate" column= "Isdelete" >
                                                                          
     <column Name = "Isdelete" sql-type= "char (1)" default= "0" not-null= "true" ></column>
   </property

Using the configuration above, y and N are stored in the database, representing true and false.

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.