1

我正在尝试与 结合sprig.netASP.NET MVC3使用 PostgreSQL 作为数据库。我之前用 MySQL 成功地做到了这一点。这是我的 2 个配置文件:

1)网络配置:

<configuration>
<configSections>
<sectionGroup name="spring">
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler,     Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
</sectionGroup>
</configSections>
<!-- spring context -->
<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>
<context>
<resource uri="~/spring-dao.xml" />
</context>
</spring>
....

2) spring-dao.xml (对于 MySQL DB) 是:

 <db:provider id="DbProvider" provider="MySql.Data.MySqlClient"
 ConnectionString="Server=localhost;Database=db_movies;Uid=root;Pwd=1234;"/>
 <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject,
 Spring.Data.NHibernate32">
 <property name="DbProvider" ref="DbProvider"/> ...

注意:我引用了 Npgsql.dll。

我是否需要另一个 .DLL 以及如何更改<db:provider .../>以使用 Postgres 作为提供程序?

我通过谷歌查找示例或教程,但没有找到。

4

1 回答 1

1

Find below a working configuration of NHibernate32 and Npgsql-2.0 (2.0.11.92) for Spring.Net 1.3.2:

<db:provider id="PqSqlDbProvider" provider="Npgsql-2.0" connectionString="Server=localhost;Port=5432;User Id=nhibernate;Password=123456789;Database=Xxxxxxxx;" />

<object name="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32" dependency-check="none">
  <property name="DbProvider" ref="PqSqlDbProvider"/>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
  <property name="MappingAssemblies">
    <list>
      <value>Xxxxxxxx</value>
    </list>
  </property>

  <property
    name="HibernateProperties">
    <dictionary>        
      <entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <entry key="connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" />
      <entry key="dialect" value="NHibernate.Dialect.PostgreSQLDialect" />
      <entry key="hbm2ddl.auto" value="create-drop"/>
    </dictionary>
  </property>
</object>

<object
  name="HibernateTransactionManager"
  type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32"
  dependency-check="none">
  <property name="DbProvider" ref="PqSqlDbProvider"/>
  <property name="SessionFactory" ref="SessionFactory"/>
</object>

<!-- Attribute driven transaction manager. -->
<tx:attribute-driven transaction-manager="HibernateTransactionManager"/>

You also need to amend the web.config to load the proper .dll instead of the one referenced by Spring:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="2.0.11.92" />
    </dependentAssembly>      
  </assemblyBinding>
</runtime>
于 2012-12-27T15:15:03.533 回答