1. Establish Domain Project
Product.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate3.Domain
{
public class Product
{
/// <summary>
/// ID
/// </summary>
public virtual Guid ID { get; set; }
/// <summary>
/// 编号
/// </summary>
public virtual string Code { get; set; }
/// <summary>
/// 名称
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// 规格
/// </summary>
public virtual string QuantityPerUnit { get; set; }
/// <summary>
/// 单位
/// </summary>
public virtual string Unit { get; set; }
/// <summary>
/// 售价
/// </summary>
public virtual decimal SellPrice { get; set; }
/// <summary>
/// 进价
/// </summary>
public virtual decimal BuyPrice { get; set; }
/// <summary>
/// 备注
/// </summary>
public virtual string Remark { get; set; }
}
}
Write mapping file Product.hbm.xml
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" assembly ="NHibernate3.Domain" namespace ="NHibernate3.Domain" >
< class name ="Product" table ="T_Product" lazy ="true" >
< id name ="ID" column ="ID" type ="Guid" >
< generator class ="assigned" />
</ id >
< property name ="Code" type ="string" >
< column name ="Code" length ="50" />
</ property >
< property name ="Name" type ="string" >
< column name ="Name" length ="50" />
</ property >
< property name ="QuantityPerUnit" type ="string" >
< column name ="QuantityPerUnit" length ="50" />
</ property >
< property name ="Unit" type ="string" >
< column name ="Unit" length ="50" />
</ property >
< property name ="SellPrice" type ="decimal" >
< column name ="SellPrice" precision ="14" scale ="2" />
</ property >
< property name ="BuyPrice" type ="decimal" >
< column name ="BuyPrice" precision ="14" scale ="2" />
</ property >
< property name ="Remark" type ="string" >
< column name ="Remark" length ="200" />
</ property >
</ class >
</ hibernate-mapping >
Then, the mapping file "the Product.hbm.xml" attribute " generation method " is set to " embedded resource "
2. Create a test project
Referenced assembly "Antlr3.Runtime.dll", "Iesi.Collections.dll", "NHibernate.dll", "Remotion.Data.Linq.dll", "nunit.framework.dll", add a reference to the project's Domain
New Config directory, copy the configuration file template
hibernate.cfg.xml
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.dll provider for SQL Server -->
< hibernate-configuration xmlns ="urn:nhibernate-configuration-2.2" >
< session-factory name ="NHibernate3.Test" >
< property name ="connection.driver_class" >NHibernate.Driver.SqlClientDriver </ property >
< property name ="connection.connection_string" >
server=.;database=NHibernateDemo;integrated security=SSPI;
</ property >
< property name ="adonet.batch_size" >10 </ property >
< property name ="show_sql" >true </ property >
< property name ="dialect" >NHibernate.Dialect.MsSql2005Dialect </ property >
< property name ="use_outer_join" >true </ property >
< property name ="command_timeout" >60 </ property >
< property name ="hbm2ddl.auto" >update </ property >
< property name ="query.substitutions" >true 1, false 0, yes 'Y', no 'N' </ property >
< property name ="proxyfactory.factory_class" >NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu </ property >
< mapping assembly ="NHibernate3.Domain" />
</ session-factory >
</ hibernate-configuration >
Modify the file attributes to " Copy Always "
Copy proxyfactory type assembly "LinFu.DynamicProxy.dll" and "NHibernate.ByteCode.LinFu.dll" to the project, and modify the generation mode
Create a "NHibernateInit.cs" class file that is used to initialize the database table structure
NHibernateInit.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
// using NUnit.Framework;
namespace NHibernate3.Test
{
public class NHibernateInit
{
public static void InitTest()
{
var cfg = new NHibernate.Cfg.Configuration().Configure( " Config/hibernate.cfg.xml ");
using (ISessionFactory sessionFactory = cfg.BuildSessionFactory()) { }
}
}
}
3. Create a database structure
In SQL Server2008, it creates a database named "NHibernateDemo" of
Main method of the console, add the following code:
Console.WriteLine ( " database created successfully! " );
4. Run the program
After the run we found NHibernate automatically create a data table for us T_Product
Reproduced in: https: //www.cnblogs.com/davidgu/archive/2012/06/11/2544630.html