简体   繁体   English

Npgsql:无法将Visual Studio 2015连接到PostgreSQL

[英]Npgsql: Unable to connect Visual Studio 2015 to PostgreSQL

I'm new to PostgreSQL and trying to change my MVC 5 app from MSSQ to PostgreSQL. 我是PostgreSQL的新手,并尝试将MVC 5应用程序从MSSQ更改为PostgreSQL。 I've successfully installed PostgreSQL, created a database, schema and a test table. 我已经成功安装了PostgreSQL,创建了数据库,架构和测试表。 I also successfully installed NuGet packages EntityFramework6.Npgsql (v3.1.1) and Npgsql (v3.1.7). 我还成功安装了NuGet软件包EntityFramework6.Npgsql(v3.1.1)和Npgsql(v3.1.7)。 I verified my web.config and machine.config files and Npgsql references were already created. 我验证了我的web.config和machine.config文件以及Npgsql引用已创建。 I'm using dapper to manage the data. 我正在使用dapper来管理数据。

web.config : web.config

<add name="PgSQL_Conn" providerName="Npgsql" connectionString="server=127.0.0.1;port=5432;userid=user;password=password;database=DB_Name" />

<providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>

<system.data>
    <DbProviderFactories>
    <remove invariant="Npgsql" />
    <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
    <add name="dotConnect for PostgreSQL" invariant="Devart.Data.PostgreSql" description="Devart dotConnect for PostgreSQL" type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql, Version= 7.6.714.0, Culture=neutral, PublicKeyToken=09af7300eec23701" /></DbProviderFactories>
  </system.data>

machine.config : machine.config

    <system.data>
            <DbProviderFactories><add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.1.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/><add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/><add name="Npgsql Data Provider" invariant="Npgsql" description=".NET Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=3.1.6.0, Culture=neutral, PublicKeyToken=5D8B90D52F46FDA7"/></DbProviderFactories>
</system.data>

I'm using the following code to test the connection: 我正在使用以下代码来测试连接:

string text, query;

query = @"SELECT ""MOSB"".""GetText""() AS ""Text"";";

IDbConnection db = new Npgsql.NpgsqlConnection(ConfigurationManager.ConnectionStrings["PgSQL_Conn"].ConnectionString);
db.Open();

var result = db.Query(query);

text = result.First().Text;

When I debug the application, the following error shows up when the breakpoint hits db.Open(); 当我调试应用程序时,当断点命中db.Open()时,将显示以下错误:

An exception of type 'Npgsql.PostgresException' occurred in Npgsql.dll but was not handled in user code Additional information: External component has thrown an exception. Npgsql.dll中发生了'Npgsql.PostgresException'类型的异常,但未在用户代码中处理。 其他信息:外部组件引发了异常。

BTW, if I try to add a new Data Connection or Server from the Server Explorer tab, I don't see any PostgreSQL Provider in the list. 顺便说一句,如果我尝试从“服务器资源管理器”选项卡添加新的数据连接或服务器,则在列表中看不到任何PostgreSQL提供程序。 If I run odbcad32.exe, two drivers appear. 如果我运行odbcad32.exe,则会出现两个驱动程序。

I really don't know what else to do. 我真的不知道该怎么办。

Problem solved! 问题解决了! The issue was that my Windows user account wasn't able to edit machine.config file. 问题是我的Windows用户帐户无法编辑machine.config文件。 After privileges were granted, file was successfully modified and now I can connect with Dapper and EF. 授予特权后,文件已成功修改,现在我可以连接Dapper和EF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM