2

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
sgy
  • 2,922
  • 2
  • 35
  • 42

3 Answers3

3

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

BlackR2D
  • 46
  • 4
  • I was having the same problem as elbaid so I tried your suggestion and now I get `MonoDevelop.Database.Sql.SqlMetalExecException: sqlmetal: Unable to connect to any of the specified MySQL hosts.` – Nick Strupat Apr 26 '11 at 03:59
  • Lattest version: --with-dbconnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.3.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" – ikutsin Jun 26 '11 at 21:53
  • Yes, that would be the latest version. Can you accept my answer then? I believe it solves the problem perfectly as the core is the fact that default settings are trying to use ByteFX connector and failing. ;) – BlackR2D Jul 16 '11 at 12:31
  • @nick - well, check the connection settings, if it's recognising the connector assembly everything is firing up, it's reporting inability to connect to host. – BlackR2D Jul 16 '11 at 12:32
  • It was not obvious for me, but to get the token for a specific version of MySQL Connector/NET, we can look in the GAC directory for MySql.Data and within this we have one (or more) directory like: "6.5.4.0__c5687fc88969c44d" and the token is the "c5687fc88969c44d" part for version 6.5.4. Maybe this may change for a future version ? Someone can confirm please ? – sgy Mar 11 '12 at 04:25
1
  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'
Alasdair McLeay
  • 2,572
  • 4
  • 27
  • 50
0

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
ikutsin
  • 1,118
  • 15
  • 22