How to use DbLinq SqlMetal for MySQL on Mac? - mysql

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.

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

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
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
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
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
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'

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

Related

EF6 and MySQL Generate Database from Model

I've tried every solution I've found on the web with no success. I continually get the error:
Running transformation: System.InvalidOperationException: The SSDL generated by the activity
called 'CsdlToSsdlAndMslActivity' is not valid and has the following errors:
No Entity Framework provider found for the ADO.NET provider with invariant name
'MySql.Data.MySqlClient'. Make sure the provider is registered in the 'entityFramework'
section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for
more information.
I have an empty Console project and a very basic model to test this with. Through the NuGet Package Console I'd executed: Install-Package MySQL.Data.Entities which installed EF6, MySQL.Data and MySQL.Data.Entities and hooked up all the references and copied the relevant DLL's into the bin/Debug folder.
I wired up the provider in the following (entire config file listed)
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6"></provider>
</providers>
</entityFramework>
</configuration>
As far as I can tell I have the provider registered. I have the latest MySQL connector updated as well. Where am I going wrong?
To support full functionality EF Designer requires 3 things:
EF runtime provider (you seem to have it)
DDEX provider for your database - DDEX is the way VS abstracts databases and as a result can talk to any database. Note that this is not something that is EF specific but EF Designer relies on it (again you seem to have it otherwise you would not be able to select MySQL connection)
Additional components (T4 templates/Workflow activities) required to support Model First flow - these are EF specific but typically installed as part of the DDEX provider - you seem to be missing this or you did not select the T4 template/workflow specific to your provider (open the edmx file and go to the properties window there are two options there that are interesting from DDL generation perspective - the "Database Generation Workflow" and the "DDL Generation Template". Click the dropdowns to see if you can select something that is MySQL specific)
Because you don't seem to have or use the workflow/T4 for MySQL (note I don't know if this is because you did not select the correct T4/workflow (see above) or MySQL DDEX does not support this or the installation is for whatever reason broken) EF Designer is using the default workflow which is for Sql Server. The default workflow relies on being able to find EF providers (for SqlServer and SqlServerCe) in the default path but the provider for MySQL is not there - hence the error. Note that the workflow and T4 templates for creating DDL are provider specific so even if you copied the MySQL provider to the default path it would not really work - you would get a peculiar result of SQL Server specific SQL Script using MySQL types.
We have a bug on improving this a little bit by providing a better guidance instead of just invoking the default DDL generation which we know will not work. You can also find some details in this bug.
I would suggest checking if MySQL components you are using do support Model First. An alternative would be just to move to Code First which only requires the EF runtime provider you already have to generate the DDL.
The other answer already kind of says this, but it's buried in his long post.
Notice that the error message refers to SSDLToSQL10.tt. You don't want that .tt file, you want the MySQL one. In the EF designer properties window, find the DDL Generation Template property and change it from SSDLToSQL10.tt to SSDLToMySQL.tt.

In my server saying "Could not load file or assembly 'MySql.Web..." but I have mysql in my application

Parser Error Message: Could not load file or assembly 'MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.
...
Line 240: <providers>
Line 241: <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.7.4.0,
.....
Resolved to putting the tag in the web.config file the following line:
<remove name="MySQLMembershipProvider"/>
I do not have access to the server settings to remove mysql. But there is another way to solve this that does not happen again?
I got this error when deploying a website to the server (but not when debugging).
To fix it, select References -> MySql.Data and set the Copy Local parameter to True. When deployed on the server you can verify this by the presence of the MySql.Data.dll in the bin folder.
Actually I believe if you remove or comment out the line with
<add name="MySQLRoleProvider" />
see http://forums.asp.net/t/1928379.aspx for more details.
This error appears in general when you provide the DLL-File in the wrong CPU-Type. So maybe you supplied x86 but you need x64. But I'm not an expert in web-development. I know this error from developing .net-Services.
secure_file_priv may be set as follows:
If empty, the variable has no effect.
If set to the name of a directory, the server limits import and export operations to work only with files in that directory. The directory must exist; the server will not create it.

Using ASP.NET with MySQL as default database?

Im creating a ASP.NET site, but i dont want to use SqlServer i want to use my pre-existing MySQL database as the default connection. ive set up a new schema for it and assigned a user, and changed my default connection string to this
<add name="DefaultConnection" connectionString="Server=THEIP;Database=THESCHEMANAME;Uid=THEUSER;Pwd=THEPASSWORD;" providerName="MySql.Data.MySqlClient" />
It seems to be able to connect to the DB alright (as far as i can tell?)
so now i figured i needed to change the default membership provider so that the login is ran through this connection, i did some looking around on the net and found this on the MySQL site
<add name="DefaultMembershipProvider" autogenerateschema="true" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.0.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="DefaultConnection" />
So i replaced my default membership provider with this, ran the site from Visual Studio, went to /Account/Register.aspx (every other page works alright as of yet) and i get this error
Could not load file or assembly 'MySql.Web, Version=6.0.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Can anybody point out what ive done wrong?
The version of the assembly was wrong, i went into the refrence assemblies and found that the version is not '6.0.4.0' but is infact '6.6.5.0', changed the version no. and its all working fine now.

MySQL connection in ASP.NET MVC 4 and Entity Framework 5.0 - multiple DbProviderFactories

I have an ASP.NET application that I would like to upload to a host that offers a MySQL database. I am using Visual Studio 2012, .NET 4.5, Entity Framework 5.0, and I have installed MySQL for Visual Studio 1.0.2. I have been trying to connect to a local MySQL server, but I have encountered some problems.
At first I referenced MySql.Data and MySql.Data.Entity at version 6.6.5.0 in my project. But when I tried to do Update-Database in the package manager console, I got the following error:
System.DateTime System.Data.Entity.Migrations.Model.InsertHistoryOperation.get_CreatedOn()
This seems to be a problem with Entity Framework 5.0 and earlier versions of Connector/.Net - the MySQL connector for visual studio. Since the process was one of discovery, I unfortunately can't recall how my project came to reference MySql.Data and MySql.Data.Entity version 6.6.5.0.
I deleted the references, and then downloaded and referenced the same assemblies in version 6.7.4.0, using NuGet. This solved the error I got before, but now Update-Database gives me the following error:
Could not load file or assembly 'MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies.
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I have solved this problem by including a specific DbProviderFactory in my Web.Config file:
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider"
invariant="MySql.Data.MySqlClient"
description=".Net Framework Data Provider for MySQL"
type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
If I leave out the remove tag, I get the error:
Column 'InvariantName' is constrained to be unique. Value 'MySql.Data.MySqlClient' is already present.
I think this means that somehow my project still has a DbProviderFactory that serves version 6.6.5.0 of the MySQL libraries. But when I search the text files of my project, I can't find the string 6.6.5.0 anywhere.
At the moment this solution lets me connect to my local MySQL server, but when I publish the project to my hosting provider and go to the web site in my browser, I just get an error message. I would like to know how to remove the implicit 6.6.5.0-serving DbProviderFactory, both as part of my learning and because I think my workaround could be the reason that my web site doesn't work at my hosting provider.
As a clue in finding a solution, I find it suspicious that when I try to add a reference, I can only see (several versions of) 6.6.5.0:
But when I go to the NuGet manager, I only have version 6.7.4.0 installed:
Hope this helps.
I wrote a blog post on this subject, my project is available for download at the bottom of this paper.
I had to install (as noted in my article) MySqlDataEntity -Pre, which install the beta version 6.7.2 beta, to get good compatibility with EF 5, but I see that now the current version is noted 6.7.4, so I updated my project accordingly, and it works just as well, so I also updated the project to download from my blog.
Hope this can help you to start with EF and MySql.
BTW, in case this is not clear, you have to install, from NuGet, MySql.Data.Entities, which will also install MYSql.Data, as Entities is depending on it.

Shared Hosting + Entity Framework + MySQL = Data Provider Errors?

long-time listener, first-time caller!
I’m trying to connect to MySQL using the ADO.NET Entity Framework. (Visual Studio 2010, .NET 4.0) I created a web project and another class library project to serve as my DAL. On my local machine, I have no problems, and I have also tried running with Medium trust. I’m able to fully control the db as expected.
When I deploy to my host (shared, medium trust) however, I keep getting errors. I have tweaked my web.config multiple ways: I’ve explicitly called my assembly in the connection string, I’ve used the wildcard mapping, I added references to the MySQL dll’s, etc.
<connectionStrings>
<add name="NamespaceContext" connectionString="metadata=res:// Namespace.Data.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null/DataModel.csdl|res:// Namespace.Data.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null/DataModel.ssdl|res:// Namespace.Data.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null/DataModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=myserver;User Id=user;database=thedatabase;password=password;Persist Security Info=True"" providerName="System.Data.EntityClient"/>
<add name=" NamespaceContext " connectionString="metadata=res://*/;provider=MySql.Data.MySqlClient;provider connection string=" server=myserver;User Id=user;database=thedatabase;password=password;Persist Security Info=True"" providerName="System.Data.EntityClient"/>
<add name=" NamespaceContext " connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=MySql.Data.MySqlClient;provider connection string=" server=myserver;User Id=user;database=thedatabase;password=password;Persist Security Info=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
The error I keep getting is:
System.ArgumentException: The specified store provider cannot be found in the configuration, or is not valid. --->
System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.
at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName)
at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
--- End of inner exception stack trace ---
at System.Data.EntityClient.EntityConnection.GetFactory(String providerString)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
at MyNamespace.Web.Html.TestPage.EntityFrameworkTestButton_Click(Object sender, EventArgs e)
I talked to their support guys (who aren’t devs) but they said the MySQL connector is installed and in the GAC. I tried to use the DbProviderFactories section in my web.config, like the following:
<system.data>
<DbProviderFactories>
<clear/>
<remove invariant="MySql.Data.MySqlClient"/>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.3.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
I was hoping I could control this and reference the provider in my project. I then receive the following error:
System.Data.MetadataException: Schema specified is not valid.
Errors: error 0194: All artifacts loaded into an ItemCollection must have the same version. Multiple versions were encountered.
MySql.Data.Entity.Properties.SchemaDefinition-6.0.ssdl(4,9) : error 0169: All SSDL artifacts must target the same provider. The ProviderManifestToken '5' is different from '5.0' that was encountered earlier.
error 0194: All artifacts loaded into an ItemCollection must have the same version.
(lots of the same error message was snipped for brevity)
I am at a complete loss. I tried strongly signing my DAL project, not using a DAL and just creating the .edmx in the web project, all to no avail. I could have sworn I got it working at some point, but perhaps I was delirious.
Sorry for the wall o' text. Can anyone help or shed some light?
Thanks!
What happens if you put the mysql dll in your bin folder? Alternatively, try to remove the version restriction on the mysql dll, they may not have that exact version loaded on their servers.
I had a very similar problem in my application, where I was using the MySQL connector for a MySQL database and the Entity Framework for a SQL Server database (two separate connections, one project). The MySQL.Data.Entity DLL seems to fight with the entity framework. I didn't actually need it so I removed it... but the error persisted!
Finally out of desperation I just deleted my bin and obj folders and recompiled. Problem solved. I guess something was cached or hanging around in my folders, even after I ran a clean and rebuild. Something to try anyways if someone else runs into this.