Using npgsql 12 and ef 6 together - have anyone su

2019-01-23 20:05发布

I'm trying to create a small POC for my boss about the hybrid of npgsql 12 and ef6, created a new project on visual studio created a sample database created the corresponding classes and the dbcontext yet, whenever I try and use ef to access the database I receive the folowing error:

The 'Instance' member of the Entity Framework provider type 'Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I know that it should be supported for quite some time now http://fxjr.blogspot.co.il/2013/06/initial-ef-6-support-added-to-npgsql.html

however I can't seem to get it to work, my App.Config file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://    go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework"     type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework,     Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"     requirePermission="false" />
    <!--<section name="entityFramework" type="Npgsql.NpgsqlFactory, Npgsql,     Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />-->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient"     type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlFactory, Npgsql" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider"
            invariant="Npgsql"
            description="Data Provider for PostgreSQL"
            type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="CoolestPGSoft"
          connectionString="Server=127.0.0.1;Port=5432;Database=CoolestPGSoft;User Id=postgres;Password=********;"
          providerName="Npgsql" />
  </connectionStrings>
</configuration>

any help would be appreciated!

4条回答
贪生不怕死
2楼-- · 2019-01-23 20:29

Now it only works with last beta version of Npgsql http://pgfoundry.org/frs/download.php/3494/Npgsql2.0.13.91-bin-ms.net4.5Ef6.zip And you must change

  <provider invariantName="Npgsql" type="Npgsql.NpgsqlFactory, Npgsql" /> 

to

 <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql" />
查看更多
再贱就再见
3楼-- · 2019-01-23 20:36

I got it EF6 and Npgsql to work following:

Entity Framework 6 with Npgsql

PMC> Install-Package EntityFramework
(should give you version 6)

PMC> Install-Package Npgsql.EF6 -Pre
(should give you 2.0.12-pre4)

And these go into App.config

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

  <connectionStrings>
    <add name="PostgreSQL" 
         providerName="Npgsql" 
         connectionString="Server=asdf;Port=5432;User Id=asdf;Password=asdf;Database=asdf;enlist=true" />
  </connectionStrings>

  <entityFramework>
    <providers>
      <provider invariantName="Npgsql" 
                type="Npgsql.NpgsqlServices, Npgsql" />
    </providers>
  </entityFramework>
查看更多
狗以群分
4楼-- · 2019-01-23 20:40

This is a template App.config which you can use as a starting point.

<xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <entityFramework>
        <providers>
          <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"></provider>
        </providers>
        <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />
      </entityFramework>
      <system.data>
        <DbProviderFactories>
          <remove invariant="Npgsql" />
          <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
        </DbProviderFactories>
      </system.data>
    </configuration>

Note that you will need the Npgsq.EntityFramework.dll 2.1.0 assembly as well as Npgsql 2.1.0 Both are currently in Beta and you can find them at Nuget or http://downloads.npgsql.org or in our github project page: https://github.com/npgsql/Npgsql/releases.

I just wrote a blog post about it: http://fxjr.blogspot.com.br/2014/02/using-entity-framework-6-with-npgsql-210.html

I hope it helps.

查看更多
Animai°情兽
5楼-- · 2019-01-23 20:46

This is how I got it working :

First install the package

Install-Package Npgsql.EF6 -Pre

And after that add this line to the web.config

<system.data>
  <DbProviderFactories>
     <remove invariant="Npgsql" />
     <add name="Npgsql Data Provider" invariant="Npgsql" support="FF"           description=".Net Framework Data Provider for Postgresql"  type="Npgsql.NpgsqlFactory, Npgsql" />
     </DbProviderFactories>
</system.data>
查看更多
登录 后发表回答