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.