Nhibernate 简单实例(一)

今天来做一个Nhibernate简单的例子,也记录下自己学习的过程,此系列会不定时更新,希望能和园友共同学习。。。。。。

1.我们先从Nhibernate的官网(http://nhibernate.info/)来下载我们要的资源,也可以从Nuget包管理工具直接搜索安装。

2.打开我们的数据库来创建一个简单的表。

CREATE TABLE [dbo].[Customer](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FirsetName] [nvarchar](50) NULL,
    [LastName] [nvarchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

3.打开我们的VS创建一个类库项目,并将关系映射文件复制到该项目中

  3.1 有两个自动生成工具 动软代码生成器 或者 codesmith 来根据模板来生成我们要的文件,下面我们根据动软来生成

       首先先链接到我们的数据库,然后选择我们要操作的表,右键 选择 模板代码生成 右边有一个 代码模板  选项 找到 Nhibernate下的 Entity.cmt 右键 打开生成 生成代码 就可以了。

       

       4.同样的过程 这次选择XmlMapping.cmt 

       5. 将两个生成的类copy到类库项目中,并修改.xml文件的属性,必须修改,否则会找不到文件

           

         6.然后我们在建立一Web项目,并应用我们要的Nhibernate.dll和Iesi.Collections以及类库项目,配置config

           

        到现在我们已经基本完成了准备工作,然后来测试下是否好用。

        7.先下一个Helper方法,方便调用Nhibernate提供的Api。

        

 1  public class NhibernateHelper
 2     {
 3         private ISessionFactory _sessionFactory;
 4         public NhibernateHelper()
 5         {
 6             _sessionFactory = GetSessionFactory();
 7         }
 8 
 9         private ISessionFactory GetSessionFactory()
10         {
11             return (new Configuration()).Configure().BuildSessionFactory();
12         }
13 
14         public ISession GetSession()
15         {
16             return _sessionFactory.OpenSession();
17         }
18 
19     }

        8.测试一下增、删、改、查

     

 1  NhibernateHelper nhelper = new NhibernateHelper();
 2         /// <summary>
 3         /// 添加
 4         /// </summary>
 5         /// <returns></returns>
 6         public ActionResult Add()
 7         {
 8             ISession session = nhelper.GetSession();
 9             Customer cModel = new Customer();
10             cModel.FirsetName = "静静";
11             cModel.LastName = "";
12             session.Save(cModel);
13             session.Flush();
14             return Content("添加成功");
15         }
16 
17         /// <summary>
18         /// 修改
19         /// </summary>
20         /// <returns></returns>
21         public ActionResult Update()
22         {
23             int customerId = 1;
24             ISession session = nhelper.GetSession();
25             Customer ct=session.Get<Customer>(customerId);
26             ct.FirsetName = "龙龙";
27             session.Update(ct);
28             session.Flush();
29             return Content("修改成功");
30         }
31 
32         /// <summary>
33         /// 删除
34         /// </summary>
35         /// <returns></returns>
36         public ActionResult Delete()
37         {
38             int customerId=2;
39             ISession session = nhelper.GetSession();
40             Customer ct = session.Get<Customer>(customerId);
41             session.Delete(ct);
42             session.Flush();
43             return Content("删除成功");
44         }
45 
46         /// <summary>
47         /// 获取列表
48         /// </summary>
49         /// <returns></returns>
50         [HttpGet]
51         public JsonResult GetCustomerList()
52         {
53             ISession session = nhelper.GetSession();
54             IList<Customer> qCustomer= session.CreateQuery("from Customer").List<Customer>();
55             return Json(qCustomer,JsonRequestBehavior.AllowGet);
56         }

          9.亲测通过,就不贴图了,这个简单的例子到这里就结束了,也没有过多的讲解,下节继续。

         

原文地址:https://www.cnblogs.com/ypyhy/p/6928651.html