How to use DbLinq SqlMetal for MySQL on Mac?

3k views Asked by At

I'm using Mono/MonoDevelop on Mac OS, and I want an example for using SqlMetal (DbLinq version included with Mono) for LINQ-to-SQL with MySQL database.

Oddly, I found many examples for SQLite, but none for MySQL. Examples found for MySQL seems to refer to the Microsoft's SqlMetal.exe.

I tried to arrange my sqlmetal command from SQLite to adapt it for MySQL, but I have this message:

sqlmetal: Could not load databaseConnectionType type 'ByteFX.Data.MySqlClient.MySqlConnection, ByteFX.Data'. Try using the --with-dbconnection=TYPE option.

Help is appreciated! Thank you.

3

There are 3 answers

5
BlackR2D On BEST ANSWER

Find and edit your sqlmetal.exe.config and replace the settings not to use ByteFX (LONG unsupported) and use MySql.Data.

Remember to include the fully qualified assembly name, in my case that line looks like:

provider name="MySQL" dbLinqSchemaLoader="DbLinq.MySql.MySqlSchemaLoader, System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" databaseConnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.2.3.0, Culture=neutral, PublicKeyToken=2f3544035097bf97"

(I have MySql Connector installed with that version and token)

Regards

0
Alasdair McLeay On
  1. Download and install MySQL Connector/Net as per http://www.mono-project.com/MySQL

    cd path_to_your MySql.Data.dll assembly
    gacutil -i MySql.Data.dll
    
  2. Browse to /Library/Frameworks/Mono.framework/Versions/2.10.12/lib/mono/gac/MySql.Data (or change the version number to match the version you're using) and you should see directories such as 6.6.5.0__c5687fc88969c44d

  3. Open /Library/Frameworks/Mono.framework/Versions/2.10.12/lib/mono/4.0/sqlmetal.exe.config (or equivalent for the version you're using) and edit the databaseConnection attribute of the <provider name="MySQL" node as per BlackR2D's answer, using the properties from the folder in step 2 e.g. Version=6.6.5.0 and PublicKeyToken=c5687fc88969c44d

  4. Make sure the path to the project you're running doesn't have a space in it or you may get a 'MonoDevelop.Database.Sql.SqlMetalExecException: sqlmetal: Could not find file' error

  5. When you select 'Generate Linq Class' change the language to C# rather than C#2 otherwise you may get a 'MonoDevelop.Database.Sql.SqlMetalExecException: sqlmetal: Object reference not set to an instance of an object' error

I can then generate Output.cs, but still get an error if trying to generate DBML (Output.dbml):

'MonoDevelop.Database.Sql.SqlMetalExecException: sqlmetal: Access to the path "//Output.dbml" is denied'
0
ikutsin On

I got this working on mono 2.10.2:

[mono-] ~ @ sqlmetal /namespace:MonoService /provider:MySql "/conn:Server=[server];Database=[db];Uid=[name];Pwd=[pass];" /code:ProxyContext.cs --with-dbconnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"

Before I put MySql.Data into the gac:

gacutil -i MySql.Data.dll