3

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

Amine Kerkeni
  • 914
  • 3
  • 14
  • 29
  • 1
    This is a very SqlMetal-specific question - I have no knowledge about it (you probably want to tag this question for it). If you what you need is a class name for the DbConnection type, then it's Npgsql.NpgsqlConnection. For anything further you're going to need to provide more details about SqlMetal. – Shay Rojansky Apr 23 '16 at 19:34
  • I tried an alternative path, generate the model under visual studio and copy the output files to MonoDevelop/Linux, it compiles but I am struggling in passing the connection string to Entity Framework. – Amine Kerkeni Apr 23 '16 at 19:59

1 Answers1

2

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.
PiKos
  • 1,344
  • 1
  • 16
  • 15