---
title: "使用nhibernate访问postgreSQL数据库"
author: "lnj"
date: "2010-04-01T15:18:12Z"
tags: []
categories: ["编程语言"]
slug: "nhibernate-postgresql"
draft: false
lastmod: "2010-04-01T15:19:55Z"
id: 630090
url: "https://lnj.iteye.com/blog/630090"
---

使用nhibernate访问postgreSQL数据库还是比较简单的，先介绍一下开发环境。

postgreSQL 8.4
NHibernate 2.1.2.4000
Npgsql 2.0.8
Npgsql是postgreSQL的.net数据库驱动，可以从[这里](http://npgsql.projects.postgresql.org)下载。

将NHibernate所需的dll添加到引用中，然后将Npgsql驱动中的Mono.Security.dll、Npgsql.dll放到bin文件夹下。NHibernate的配置节为：

```xml

<session-factory>
        <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
        <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
        <property name="connection.connection_string">、
        <!-- nhibernate给出的配置示例中，数据库链接字符串格式有误，initial catalog应改为Database -->
Server=localhost;Database=sample;User ID=postgres;Password=postgres;</property>
        <property name="proxyfactory.factory_class">
        <!-- 选择nhibernate使用的ioc框架 -->
        NHibernate.ByteCode.Spring.ProxyFactoryFactory, NHibernate.ByteCode.Spring</property>
        <property name="current_session_context_class">managed_web</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping assembly="XXX"/>
</session-factory>

```


之后就可以编写代码访问postgreSQL数据库了。