配置ASP.NET Nhibernate

简介: web.config:配置sql server数据库 1 2 3 4 5 6 7 8 9 NHibernate.

web.config:配置sql server数据库

 1 <configuration>
 2     <configSections>
 3         <!--NHibernate Section-->
 4         <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
 5         <!--NHibernate Section End-->
 6     </configSections>
 7     <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 8         <session-factory name="NHibernate.Test">
 9             <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
10             <property name="connection.connection_string">
11                 server=127.0.0.1;database=NHibernateSample;uid=sa;pwd=zhangwei
12             </property>
13             <property name="adonet.batch_size">10</property>
14             <property name="show_sql">true</property>
15             <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
16             <property name="use_outer_join">true</property>
17             <property name="command_timeout">60</property>
18             <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
19             <property name="proxyfactory.factory_class">
20                 NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
21             </property>
22         </session-factory>
23     </hibernate-configuration>
24 ......

web.config:配置 Oracle 数据库

 1 <configuration>
 2     <configSections>
 3         <!--NHibernate Section-->
 4         <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
 5         <!--NHibernate Section End-->
 6     </configSections>
 7     <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
 8         <session-factory>
 9             <property name="connection.provider">
10                 NHibernate.Connection.DriverConnectionProvider
11             </property>
12             <property name="connection.driver_class">
13                 NHibernate.Driver.OracleClientDriver
14             </property>
15             <property name="dialect">
16                 NHibernate.Dialect.Oracle10gDialect
17             </property>
18             <property name="connection.connection_string">
19                 Data Source=155ZNXJ;User ID=cxm;Password=cxm
20             </property>
21             <property name="proxyfactory.factory_class">
22                 NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
23                 NHibernate.ByteCode.LinFu
24             </property>
25             <property name="show_sql">true</property>
26             <property name="query.substitutions">
27                 true 1, false 0, yes 'Y', no 'N'
28             </property>
29         </session-factory>
30     </hibernate-configuration>
31 ......

 

SessionFactory:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using NHibernate;
 7 using NHibernate.Cfg;
 8 
 9 namespace MyMvcDemo.Nhibernate
10 {
11     public class SessionFactory
12     {
13         private ISessionFactory _sessionFactory;
14 
15         public SessionFactory()
16         {
17             _sessionFactory = GetSessionFactory();
18         }
19 
20         private void Init()
21         {
22             var config = new Configuration();
23             config.AddAssembly("MyMvcDemo.Nhibernate");
24             config.Configure();
25             _sessionFactory = config.BuildSessionFactory();
26         }
27 
28         private ISessionFactory GetSessionFactory()
29         {
30             if (_sessionFactory == null)
31                 Init();
32 
33             return _sessionFactory;
34         }
35 
36         public ISession GetSession()
37         {
38             return _sessionFactory.OpenSession();
39         }
40     }
41 }

Customer.hbm.xml:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyMvcDemo.Model" namespace="MyMvcDemo.Model.Customer">
 3     <class name ="Customer" table="Customer" lazy="false">
 4         <id name="CustomerId">
 5             <generator class ="native"/>
 6         </id>
 7 
 8         <property name="FirstName"/>
 9         <property name ="LastName"/>
10     </class>
11 </hibernate-mapping>

Customer.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace MyMvcDemo.Model.Customer
 7 {
 8     public class Customer
 9     {
10         public int CustomerId { get; set; }
11         public string FirstName { get; set; }
12         public string LastName { get; set; }
13         public string Version { get; set; }
14     }
15 }

HomeController.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web.Mvc;
 6 using NHibernate;
 7 using NHibernate.Cfg;
 8 using MyMvcDemo.Service;
 9 using MyMvcDemo.Nhibernate;
10 
11 namespace MyMvcDemo.Controllers
12 {
13     public class HomeController : Controller
14     {
15         private readonly ISession _session;
16         readonly SessionFactory _sessionFactory = new SessionFactory();
17         private readonly CustomerService _customerService;
18 
19         public HomeController()
20         {
21             _session = _sessionFactory.GetSession();
22             _customerService = new CustomerService(_session);
23         }
24 
25 
26         public ActionResult Index()
27         {
28             var customer = _customerService.GetCustomerById(1);
29             ViewData["Message"] = customer.FirstName + customer.LastName;
30             return View();
31         }
32 
33         public ActionResult About()
34         {
35             ViewData["Message"] = "Hello About";
36             return View();
37         }
38     }
39 }

CustomerService.cs:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NHibernate.Cfg;
 6 using NHibernate;
 7 using MyMvcDemo.Nhibernate;
 8 using MyMvcDemo.Model.Customer;
 9 using MyMvcDemo.Service;
10 using NHibernate.Criterion;
11 
12 namespace MyMvcDemo.Service
13 {
14     public class CustomerService
15     {
16         private ISession _session;
17         public ISession Session
18         {
19             set { _session = value; }
20         }
21 
22         /// <summary>
23         /// 初始化Session
24         /// </summary>
25         /// <param name="session"></param>
26         public CustomerService(ISession session)
27         {
28             _session = session;
29         }
30 
31         /// <summary>
32         /// 添加Customer
33         /// </summary>
34         /// <param name="customer"></param>
35         public void CreateCustomer(Customer customer)
36         {
37             _session.Save(customer);
38             _session.Flush();
39         }
40 
41         /// <summary>
42         /// 根据id获取Customer
43         /// </summary>
44         /// <param name="customerid"></param>
45         /// <returns></returns>
46         public Customer GetCustomerById(int customerid)
47         {
48             return _session.Get<Customer>(customerid);
49         }
50 
51         public IList<Customer> GetCustomerByFirstName(string name)
52         {
53             return _session.CreateCriteria(typeof(Customer))
54                            .Add(Restrictions.Eq("FirstName", name))
55                            .List<Customer>();
56         }
57     }
58 }

Index.aspx:

 1 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
 2 
 3 <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 4     Home Page
 5 </asp:Content>
 6 
 7 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 8     <h2><%: ViewData["Message"] %></h2>
 9     <p>
10         To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
11     </p>
12 </asp:Content>

运行结果:

 

 

目录
相关文章
|
6月前
|
开发框架 JSON .NET
ASP.NET Core 自定义配置警告信息
自定义配置警告信息需要在 startup 类中的 ConfigureService 方法中进行配置示例: // 注册 控制器服务 services.AddControllers(configure: setup => { setup.ReturnHttpNotAcceptable = true; ...
43 0
|
3月前
|
IDE 前端开发 JavaScript
【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
【1月更文挑战第26天】【C#】C# 开发环境配置(Rider 一个.NET 跨平台集成开发环境)
|
4月前
|
开发框架 .NET PHP
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
Web Deploy配置并使用Visual Studio进行.NET Web项目发布部署
|
4月前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
|
4月前
|
JSON JavaScript 前端开发
全面的.NET微信网页开发之JS-SDK使用步骤、配置信息和接口请求签名生成详解
全面的.NET微信网页开发之JS-SDK使用步骤、配置信息和接口请求签名生成详解
|
4月前
|
SQL Shell 数据库
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
|
6月前
|
存储 开发框架 .NET
ASP.NET Core 配置
ASP.NET Core (Startup) StartupASP.NET Core必须包含Startup类。它就像 Global.asax 文件,我们传统的 .NET 应用程序。如名称建议的那样,在应用程序启动时首先执行它。在程序类的Main方法中配置主机时,可以使用**UseStartup()**扩展方法配置启动类。请查看下面的程序类,并重点介绍 WebBuilder.UseStart...
28 0
ASP.NET Core 配置
|
7月前
|
容器
.NET Core - 选项框架:服务组件集成配置的最佳实践
.NET Core - 选项框架:服务组件集成配置的最佳实践
|
7月前
|
存储
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
.NET Core - 自定义配置数据源:低成本实现定制化配置方案
|
7月前
|
JSON 数据格式
.NET Core - 配置绑定:使用强类型对象承载配置数据
.NET Core - 配置绑定:使用强类型对象承载配置数据