Search code examples
c#linqmonodevelopnpgsqlsqlmetal

Generate linq classes using sqlmetal for npgsql


I am trying to generate c# classes for Linq (or Entity Framework 6) from an existing PostgreSql database on a Linux . I installed npgsql and Entity Framework 6 in a monodevelop project and I was able to query the database with pure SQL.

But when I try to generate Linq classes using Sqlmetal:

sqlmetal -c "Port=5432;Encoding=UTF-8;Server=myDbServer;Database=MyDatabase;UserId=myUser;Password=mypassword;" --code=model.cs --language=c# --provider=PostgreSql

I get the following error:

sqlmetal: Could not load databaseConnectionType type 'npgsql'. Try using the --with-dbconnection=TYPE option.

I don't know what to put after --with-dbconnection


Solution

  • Currently Npgsql driver is not distributed with Mono (http://www.mono-project.com/docs/about-mono/releases/4.0.0/). There are few possibilites to solve this problem:

    1. Install Npgsql.dll into GAC. To do this, you have to use command:

      sudo gacutil -i Npgsql.dll
      Npgsql.dll can be retrieved from here: https://github.com/npgsql/npgsql/releases/download/v2.2.5/Npgsql-2.2.5-net45.zip. (Version 2.2.5.0 is given as an example, different version (for example v3) can also be used). After that you can try to rerun sqlmetal
      sqlmetal -c "Port=5432;Encoding=UTF-8;Server=myDbServer;Database=MyDatabase;UserId=myUser;Password=mypassword;" --code=model.cs --language=c# --provider=PostgreSql --with-dbconnection="Npgsql.NpgsqlConnection, Npgsql, Version=2.2.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"
      

    2. Put Npgsql.dll in some folder and add this folder to MONO_PATH env variable. For example, put Npgsql.dll into /usr/local/lib/mono-additional-assemblies and then

      set MONO_PATH=$MONO_PATH:/usr/local/lib/mono-additional-assemblies
      After that rerun sqlmetal without --with-dbconnection. Should work.