Using Entity Framework 6 with Npgsql 2.1.0

UPDATE (2014-05-19): Marek Beneš noticed a problem in the default connection factory config. It is fixed now. Thanks, Marek!

UPDATE (2014-02-20): I created a new post explaining how to get Npgsql 2.1.0.

Although this post is about EF 6, I'd like to talk about our current situation to support both EF 6 and EF4.x which explain why there are some subtle changes between EF 4.x and EF 6.x App.config settings. 

Support for EF versions 4.x and 6.x

Sometime after we started to work on Npgsql 2.1.0, we started to add code to support EF6 and decided to reorganize our Entity Framework support code. Shay created a pull request to organize this change and isolate the EF code out of core Npgsql code. The result was the creation of two separated assemblies:

  • Npgsql.EntityFramework.dll for EF6 and above;
  • Npgsql.EntityFrameworkLegacy.dll for EF4.x.

Only when using Npgsql with EF6 you will need to reference Npgsql.EntityFramework.dll assembly. This is needed because the EF system has to find it in order to use it.

When using EF 4.x, you won't need to reference Npgsql.EntityFrameworkLegacy.dll. Npgsql will load it using reflection. You just need to make sure the assembly is located at the same place as Npgsql.dll. With this approach, using EF 4.x with 2.1.0 will be the same way as using with previous versions of Npgsql.

In order to facilitate how to configure Npgsql 2.1.0 to work with EF6, I'm pasting* below an App.config template which you can use as a starting point(1).

Configuration settings for EF 6

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

*Sorry for the bad formatting :(

The most important change from previous configuration files is the provider entry which had its type changed from Npgsql.NpgsqlFactory to Npgsql.NpgsqlServices as well as the location of this type which used to be in the Npgsql assembly and now it is located at the Npgsql.EntityFramework assembly.

With those changes, you will be able to use Npgsql 2.1.0 with EF6 without any problem.

Let me know in the comments if you have any problem with those directions.

In a later post I'll show a step by step on how to use Npgsql 2.1.0 with EF 6 and some exciting features we are working on: Support for EFMigration and Database creation and Returning DB generated values on insert for EF


1. Another option to have those configurations set up is to install the Nuget Npgsql.EntityFramework package. Shay added a script which configures the App.config file and add the needed entries in order to make it work with Npgsql.


Anonymous said...

thanx , it is a good development

tryhp3 said...
This comment has been removed by the author.
tryhp3 said...

SSL no work

An unhandled exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in EntityFramework.dll

Additional information: An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

{"The provider did not return a ProviderManifestToken string."}
{"Failed to establish a connection to 'my host...'."}

Francisco Junior said...

Hi, Piotr Trychta!

Can you report this error in our issues section on github? This way we can track this it. Thanks in advance.

Antônio Neto said...

Aqui está dando esse erro:

Additional information: Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql' type as specified in the application configuration. See inner exception for details.

Francisco Junior said...

Hello, Antônio.

Can you post the inner exception? This would give us more information about the problem you are having.

Thanks in advance.

alexacron said...

at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at System.Data.Entity.Internal.AppConfig.<.ctor>b__3()
at System.Lazy`1.CreateValue()
at System.Lazy`1.LazyInitValue()
at System.Data.Entity.Internal.AppConfig.TryGetDefaultConnectionFactory()
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.<>c__DisplayClass1.b__0(Tuple`2 t)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass3.b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver)
at System.Data.Entity.Internal.LazyInternalConnection.Initialize()
at System.Data.Entity.Internal.LazyInternalConnection.get_ProviderName()
at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName()
at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)

alexacron said...

The comment above was the inner details of the exception
"Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql' "

As I checked in the source it has aprivate constructor, which is good, but I don't have a clue why is it called

Unknown said...
This comment has been removed by the author.
Unknown said...

you have bug in provided config file.
In the defaultConnectionFactory element, there shouldn't be Npgsql.NpgsqlFactory but Npgsql.NpgsqlConnectionFactory. This change resolves problem mentioned above.

Td said...
This comment has been removed by the author.
Td said...

Just want to share how I've wasted few hours of my life...

First I've installed from nuget:
1) Entity framework 6.1.1
2) Npgsql 2.1.3
3) Npgsql for Entity framework 2.1.3

Then I've tried to use app.config provided in the post. It didn't work for me. I was trying to fix it for few hours without any luck.

Later I just restored type in defaultConnectionFactory element, which previous commenter recommended to change. After that it worked fine!

Just in case, providing my element:
defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql"

Francisco Junior said...

Hi, Td! Thanks for your comment.

I updated the original post with Merek's comments. Did you try this most updated version or a previous version from this article?

ab.normal said...
This comment has been removed by the author.
ab.normal said...

Hello, I'm trying to get this thing to work, but I'm having some problems. The error i get is:

Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlConnectionFactory, Npgsql'

I'm using Npgsql 2.1.3

My config file looks like the one posted above.

I would appreciate any help!


Unknown said...

Hello. I follow all steps here

But still in step 8 doesn't show
[PostgreSQL Database] at Change Data Source.

What is the step making that option available?

otr214426 said...

Work from home theory is fast gaining popularity because of the freedom and flexibility that comes with it. Since one is not bound by fixed working hours, they can schedule their work at the time when they feel most productive and convenient to them. Women & Men benefit a lot from this concept of work since they can balance their home and work perfectly. People mostly find that in this situation, their productivity is higher and stress levels lower. Those who like isolation and a tranquil work environment also tend to prefer this way of working. Today, with the kind of communication networks available, millions of people worldwide are considering this option.

Women & Men who want to be independent but cannot afford to leave their responsibilities at home aside will benefit a lot from this concept of work. It makes it easier to maintain a healthy balance between home and work. The family doesn't get neglected and you can get your work done too. You can thus effectively juggle home responsibilities with your career. Working from home is definitely a viable option but it also needs a lot of hard work and discipline. You have to make a time schedule for yourself and stick to it. There will be a time frame of course for any job you take up and you have to fulfill that project within that time frame.

There are many things that can be done working from home. A few of them is listed below that will give you a general idea about the benefits of this concept.

This is the most common and highly preferred job that Women & Men like doing. Since in today's competitive world both the parents have to work they need a secure place to leave behind their children who will take care of them and parents can also relax without being worried all the time. In this job you don't require any degree or qualifications. You only have to know how to take care of children. Parents are happy to pay handsome salary and you can also earn a lot without putting too much of an effort.

For those who have a garden or an open space at your disposal and are also interested in gardening can go for this method of earning money. If given proper time and efforts nursery business can flourish very well and you will earn handsomely. But just as all jobs establishing it will be a bit difficult but the end results are outstanding.

Freelance can be in different wings. Either you can be a freelance reporter or a freelance photographer. You can also do designing or be in the advertising field doing project on your own. Being independent and working independently will depend on your field of work and the availability of its worth in the market. If you like doing jewellery designing you can do that at home totally independently. You can also work on freelancing as a marketing executive working from home. Wanna know more, email us on and we will send you information on how you can actually work as a marketing freelancer.

Internet related work
This is a very vast field and here sky is the limit. All you need is a computer and Internet facility. Whatever field you are into work at home is perfect match in the software field. You can match your time according to your convenience and complete whatever projects you get. To learn more about how to work from home, contact us today on workfromhome.otr214426@gmail.comand our team will get you started on some excellent work from home projects.

Diet food
Since now a days Women & Men are more conscious of the food that they eat hence they prefer to have homemade low cal food and if you can start supplying low cal food to various offices then it will be a very good source of income and not too much of efforts. You can hire a few ladies who will help you out and this can be a good business.

Thus think over this concept and go ahead.

Unknown said...
This comment has been removed by the author.
Unknown said...

Awsum piece of coding, just got my first code first database on PostgresQL :)

I did however have to correct the line:


defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql"


defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql.EntityFramework"

This is simple once you realise the class has moved assembly.

Natalie said...

this is very useful informative sharing thanks for this ....
free download | free software download

Carlos Amaral said...

Olá Francisco.
Estou querendo para com Java e tentando entrar no mundo dotNet.
Tenho que começar um projeto, e o BD será Postgres.
Tentei seguir vários tutoriais aqui, mas não consigo fazer funcionar.

Ambiente : Windows 7 64, VS 2013 Community, Postgres 9.3.5

Pelo NuGet, instalei o Npgsql.EntityFramework, que já baixou todas as dependências (Npsql e EntityFramework
Criei um aplicação console e estou tentando acessar um BD e uma tabela existentes mas da retorna o seguinte erro :
An unhandled exception of type 'System.ArgAn unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: O formato da cadeia de inicialização não está de acordo com a especificação iniciada no índice 0.

Meu App.config está assim :

alguma idéia do que pode estar errado

costlules said...

you dont need to know all this !
all you need to do with us is to click on links and we will pay you for this !

José de Paz said...

Hola, gracias Francisco Figueiredo por esta excelente herramienta.

Estoy utilizando lo siguiente:

Postgresql Server 9.0.18
Npgsql DDEX (2.2.5)
Visual Studio Prof 2013 Sp4

Tengo la siguiente función:

pass character varying,
users character varying)
RETURNS integer AS

COST 100;
ALTER FUNCTION "uspLogin"(character varying, character varying)
OWNER TO postgres;

Y cuando realizo el mapeo desde Visual Studio, la generación de las tablas se realiza de forma exitosa, pero para las funciones me muestra el siguiente mensaje:

Error 6046: Unable to generate function import return type of the store function 'uspLogin'. The store function will be ignored and the function import will not be generated.

Quisiera conocer la forma de realizar la importación de forma correcta.

Saludos y gracias

Unknown said...

Dear Francisco Jr.,

What do I not understand?
I have installed the Nuget packages Npgsql, Entity Framework 6.1.3 and Npgsql Entity Framework to my project in the free VS2015 Community edition. I add ADO.Net to the project. First step is to choose DB first. In second step I can't make a connection to the postgress SQL server only mS SQL servers. What am I doing wrong?

Hope someone can help

Jan Rou

Unknown said...

Jan Rou,

It sounds like you may not have edited your "app.config" to match what is in the blog article, and removed the LocalDb default connection factory for SQL Server.

Thereafter, from contributions by Marek Benes and Dave Paterson in the comments thread, you need to change the defaultConnectionFactory/@type attribute.

For the Npgsql v3.21 bits on an EF6 project, I had to change mine to

<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" ...

Derek Harmon

Unknown said...

Cara Derek e Francisco Jr, no meu caso, consigo adicionar as classe normalmente, mas quando vou "ADD();" algo no Banco ele me retorna um erro.

o que devo estar fazendo de errado ?

Obs. EF6

It's been a long time....

Hi, all!! It's been a long time since the last time I posted here... Wow, almost 6 years!!  What have I been doing all this time,...