配置ASP.NET Nhibernate
web.config:配置sql server数据库
<configuration>
<configSections>
<!--NHibernate Section-->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
<!--NHibernate Section End-->
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">
server=127.0.0.1;database=NHibernateSample;uid=sa;pwd=zhangwei
</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="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
</property>
</session-factory>
</hibernate-configuration>
......
web.config:配置 Oracle 数据库
<configuration>
<configSections>
<!--NHibernate Section-->
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate"/>
<!--NHibernate Section End-->
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
NHibernate.Driver.OracleClientDriver
</property>
<property name="dialect">
NHibernate.Dialect.Oracle10gDialect
</property>
<property name="connection.connection_string">
Data Source=155ZNXJ;User ID=cxm;Password=cxm
</property>
<property name="proxyfactory.factory_class">
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu
</property>
<property name="show_sql">true</property>
<property name="query.substitutions">
true 1, false 0, yes 'Y', no 'N'
</property>
</session-factory>
</hibernate-configuration>
......
SessionFactory:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Cfg; namespace MyMvcDemo.Nhibernate
{
public class SessionFactory
{
private ISessionFactory _sessionFactory; public SessionFactory()
{
_sessionFactory = GetSessionFactory();
} private void Init()
{
var config = new Configuration();
config.AddAssembly("MyMvcDemo.Nhibernate");
config.Configure();
_sessionFactory = config.BuildSessionFactory();
} private ISessionFactory GetSessionFactory()
{
if (_sessionFactory == null)
Init(); return _sessionFactory;
} public ISession GetSession()
{
return _sessionFactory.OpenSession();
}
}
}
Customer.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyMvcDemo.Model" namespace="MyMvcDemo.Model.Customer">
<class name ="Customer" table="Customer" lazy="false">
<id name="CustomerId">
<generator class ="native"/>
</id> <property name="FirstName"/>
<property name ="LastName"/>
</class>
</hibernate-mapping>
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MyMvcDemo.Model.Customer
{
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Version { get; set; }
}
}
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using NHibernate;
using NHibernate.Cfg;
using MyMvcDemo.Service;
using MyMvcDemo.Nhibernate; namespace MyMvcDemo.Controllers
{
public class HomeController : Controller
{
private readonly ISession _session;
readonly SessionFactory _sessionFactory = new SessionFactory();
private readonly CustomerService _customerService; public HomeController()
{
_session = _sessionFactory.GetSession();
_customerService = new CustomerService(_session);
} public ActionResult Index()
{
var customer = _customerService.GetCustomerById();
ViewData["Message"] = customer.FirstName + customer.LastName;
return View();
} public ActionResult About()
{
ViewData["Message"] = "Hello About";
return View();
}
}
}
CustomerService.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Cfg;
using NHibernate;
using MyMvcDemo.Nhibernate;
using MyMvcDemo.Model.Customer;
using MyMvcDemo.Service;
using NHibernate.Criterion; namespace MyMvcDemo.Service
{
public class CustomerService
{
private ISession _session;
public ISession Session
{
set { _session = value; }
} /// <summary>
/// 初始化Session
/// </summary>
/// <param name="session"></param>
public CustomerService(ISession session)
{
_session = session;
} /// <summary>
/// 添加Customer
/// </summary>
/// <param name="customer"></param>
public void CreateCustomer(Customer customer)
{
_session.Save(customer);
_session.Flush();
} /// <summary>
/// 根据id获取Customer
/// </summary>
/// <param name="customerid"></param>
/// <returns></returns>
public Customer GetCustomerById(int customerid)
{
return _session.Get<Customer>(customerid);
} public IList<Customer> GetCustomerByFirstName(string name)
{
return _session.CreateCriteria(typeof(Customer))
.Add(Restrictions.Eq("FirstName", name))
.List<Customer>();
}
}
}
Index.aspx:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: ViewData["Message"] %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>
</asp:Content>
运行结果:

配置ASP.NET Nhibernate的更多相关文章
- 在Linux(Ubuntu/openSUSE/CentOS)下配置ASP.NET(Apache + Mono)
[题外话] 闲的无聊竟然想尝试测试自己做的项目在不同操作系统上的性能表现,所以决定试试在Linux上部署Apache和Mono的环境.由于平时很少接触Linux,所以从网上找了几篇文章(附在相关链接中 ...
- 用"hosting.json"配置ASP.NET Core站点的Hosting环境
通常我们在 Prgram.cs 中使用硬编码的方式配置 ASP.NET Core 站点的 Hosting 环境,最常用的就是 .UseUrls() . public class Program { p ...
- [原创]IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135)
IIS7.5下配置ASP+PHP环境及错误处理(0xc0000135) http://user.qzone.qq.com/93701178/blog/1398155812 操作系统更新至Win7或Wi ...
- 配置ASP.NET Web应用程序, 使之运行在medium trust
这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust. 如果你的服务器有多个应用程序, 你可以使用code access security和medium ...
- 如何在IIS7下配置ASP+ACCESS环境
如何在IIS7下配置ASP+ACCESS环境 | 浏览:901 | 更新:2013-01-16 17:46 1 2 3 4 5 6 7 分步阅读 默认装完IIS7之后,使用ASP程序会发现提示数据库连 ...
- IIS配置ASP.NET和服务器错误页
以下两种方法均为全站出错处理 方法一: 1.在Web.config配置文件中<system.web></system.web>中添加<customErrors mode= ...
- Win7旗舰版中的IIS配置asp.net的运行环境
Win7旗舰版中的IIS配置asp.net的运行环境 以前弄过好多次,都没有成功,昨天晚上不知怎么地就成功了,借用我同学的一句话,这叫“灵光一闪”,废话不多说了,这个成功是有图有视频有真相地哈! ...
- window下appserv组合包配置asp标记风格与简短风格
php一共有四种编码风格 分别为 :XML风格,脚本分铬,简短风格,ASP风格 如果要配置asp标记风格与简短风格,需要在php.ini文件中配置. 打开文件的位置C:\ window\php.ini ...
- 图解win7中IIS7.0的安装及配置ASP环境
控制面板中“程序”的位置 “程序”中“打开或关闭Windows功能”的位置 如图,安装IIS7时需要选择要使用的功能模块 IIS7安装完成之后可以在开始菜单的所有程序中看到“管理工具”,其中有一个“I ...
随机推荐
- Ajax实现聊天
用Ajax发送请求,查询数据库是否有自己的数据,如果有自己的数据,就返回 前端页面 <head> <meta charset="UTF-8"> <ti ...
- Table of Contents - CXF
Getting Started A simple JAX-WS service Writing a service with Spring Tools WSDL to Java RESTful Ser ...
- 【网络收集】如何修改vs tfs的登录名和密码 .
连接TFS时,如果本机保存了用户的网络密码,不会出现用户名和密码的输入框,若要更换TFS的用户名和密码,需按以下步骤操作:控制面板--->用户账号--->管理网络密码,此时会列出所有保存了 ...
- Cocos2d-JS键盘事件
Cocos2d-JS中的键盘事件与触摸事件不同,它没有空间方面信息.键盘事件不仅可以响应键盘,还可以响应设备的菜单.键盘事件是EventKeyboard,对应的键盘事件监听器(cc.EventList ...
- CentOS 7.2 无法生成 coredump文件
CentOS版本 cat /etc/centos-release CentOS Linux release 7.2.1511 (Core) 设置ulimit -c ulimited 依旧无法生成co ...
- POJ 2533
最长上升子序列裸题在网上看到有两种方法...一种复杂度O(N^2),一种O(NlogN).orz O(N^2): #include<cstdio> #define N 1001 int m ...
- qt QLabel 显示网络图片
在网上试了很多代码都不能使用,自己写了写代码. 直接上代码 Codevoid QMusicLogo::setNetworkPic(const QString &szUrl) { QUrl ur ...
- SQL 面试题(一)
问题来自于CSDN问答,练练SQL吧. 测试数据SQL代码: if OBJECT_ID('td_ls_2') is not null drop table td_ls_2 go if OBJECT_I ...
- silverlight webclient实现上传、下载、删除、读取文件
1.上传 private void Button_Click_1(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = ...
- IOS_修改项目模板
1. /Applications/Xcode.app/Contents/Developer/Library/Xcode/Templates/File\ Templates/Source/Cocoa\ ...