2016-03-06 41 views
0

我試圖建立與EF6和3.X Npgsql的項目3 Npgsql的代碼第一次連接字符串

我採取了以下措施:

  1. 使用的NuGet安裝實體框架6
  2. 名單項目
  3. 添加連接字符串中使用此處描述語法config文件:http://www.npgsql.org/doc/connection-string-parameters.html

使用的NuGet安裝Npgsql的EntityFramework6包

當我嘗試連接到數據庫中,我得到的錯誤:

"Keyword not supported: 'port'." 

指的是連接字符串 的端口設置,如果我刪除了「港」連接失敗,錯誤:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)" 

錯誤消息好像它試圖使用SQL服務器協議而不是postgres連接。是否有任何額外的步驟需要採取以獲得此設置?

文檔中的大多數.config文件示例顯示過時,所以我想知道是否需要手動將設置添加到某處的配置。

如果任何人有這個設置工作,你能分享你的.config文件謝謝。

回答

0

你說得對:「它試圖使用SQL服務器協議而不是postgres連接」,因爲它是默認的提供者。

您必須添加提供商Npgsql的向DbProviderFactories和EF提供商在web.config中,然後在連接字符串鏈接到供應商(見providerName),所以entityframe可以連接到PostgreSQL:

<system.data> 
    <DbProviderFactories> 
     <add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" /> 
    </DbProviderFactories> 
    </system.data> 

    <entityFramework> 
    <providers> 
     <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /> 
    </providers> 
    </entityFramework> 

    <connectionStrings> 
    <add name="CS" connectionString="Server=127.0.0.1;Port=5432;Database=MYDB;User Id=postgres;Password=mypass" providerName="Npgsql" /> 
    </connectionStrings>