13

I want to use Entity Framework 6 with PostgreSQL in ASP.NET MVC 4 project. I got Entity Framework 6.0.2 + Npgsql 2.0.14.3, but I get error. How do I fix this?

Error:

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

Additional information: The 'Instance' member of the Entity Framework provider type 'Npgsql.NpgsqlServices, Npgsql, Version=2.0.14.3, 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.

Web.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="DatabaseContext" connectionString="Server=localhost;port=5432;Database=main;User Id=postgres;Password=password;" providerName="Npgsql" />
</connectionStrings>
<entityFramework>
  <providers>
    <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql" />
  </providers>
</entityFramework>
2

1 Answer 1

22

I needed to install Npgsql like this:

Install-Package Npgsql.EF6 -Pre

It installs different version which works.

UPDATE: I found that for newer beta version you can write

install-package Npgsql.EntityFramework -pre

Sign up to request clarification or add additional context in comments.

4 Comments

+1 - No idea how you figured that out, but you saved me a ton of time by following up with the answer. Thanks ;-)
I just added some more comments about the need for the Npgsql 2.1 version to get EF 6 support as well as references for the NpgsqlEntityFramework. I hope those directions will help new users to get it going faster. Thanks for the heads up!
Another detail is that starting with Npgsql 2.1.0, the line which registers the provider has to be: <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" /> for EF6 or above or <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFrameworkLegacy" /> for EF5 and below. This is needed because we moved the EF code to separated assemblies.
I got all of this working, but when trying the EF Power Tools (Reverse Engineer Code First with a postgres db) it fails with Could not load file or assembly 'Npgsql.EntityFrameworkLegacy, Version=2.2.0.0 even though I specified Npgsql.EntityFramwork for the provider (which is suppose to be for EF6)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.