So I’ve been attempted to get EntityFramework to play nice with Postgres for a site I’m creating.
A little history, I created the site initially for myself and a small group to use so I used MVC3, postgres, Mono and other tools that were… “funky”. As there was no real deadline, uptime SLAs etc. it wasn’t an issue, and I was developing it myself, so a persistence layer of my own making was fine.
Fast forward to now, and I feel it’s time to move to a more maintainable site. For this I had a choice of either NHibernate or Entity for the persistence layer. Having bought the “Pro ASP.NET MVC 3 Framework” book by Steve Sanderson and Adam Freeman, I felt that keeping as close to the examples in the book initially would be better.
So, currently (April 2012), Npgsql doesn’t include design-time support for Visual Studio, so you have to do things slightly differently. I srtuggled for a while to get it to work from some of the blogs there are around, so here’s what I found to work.
1. Make sure that you use the .NET 4.0 build of Npgsql.
currently the filename is: Npgsql220.127.116.11-bin-ms.net4.0
2. Make sure that you install both the Npgsql.dll and Mono.Security.dll into the CORRECT GAC.
The GAC for .NET is accessed using the following path:
C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe
use .\gacutil -i c:\<path to files> to install them.
3. Make sure that you alter the correct machine.config file for .NET 4.0
<add name=”Npgsql Data Provider” invariant=”Npgsql” support=”FF” description=”.Net Framework Data Provider for Postgresql Server” type=”Npgsql.NpgsqlFactory, Npgsql, Version=18.104.22.168, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7′ />
4. When adding the above XML, make sure that the “version” attribute of the type is the correct one as it is very important.
5. Make sure you use the correct EdmGen.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\EdmGen.exe /mode:FullGeneration /provider:Npgsql /c:"Server=<server>;port=<port>;UserId=<user>;Password=<password>;Database=<database>;" /project:<project>
If you’re getting this error:
error 7001: Failed to find or load the registered .Net Framework Data Provider.
or this error:
error 7001: A null was returned after calling the ‘GetService’ method on a store provider instance of type ‘Npgsql.NpgsqlFactory’. The store provider might not be functioning correctly.
Chances are that either Step 2 didn’t work, or you installed it into the wrong GAC, or, you put the wrong version in the XML.
The second error also occurs if you’re using the .NET 2.0 version.
Thanks to the following articles for getting me up and running.