Entity Framework 6 with MySQL - mysql

I'm trying to get Entity Framework 6 to work with a MySQL database. I am following this guide:
http://bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/
Where I'm replacing the local database with a MySQL one. I installed the MySQL.NET Connector/Net Nuget package. However, replacing the connection string with that of my host is giving me problems. I can't get the enable-migrations command to work properly. On their FAQ pages, my host states you need to use this connection string:
Server=myServerAddress;Database=MyDataBase;User=MyUser;Password=myPassword
Which led me to this connection string:
<add name="DefaultConnection" connectionString="Server=12.345.6.789:1234;Database=MyDatabaseName;User=MyUserName;Password=MySuperSecretPassword" providerName="System.Data.SqlClient" />
I set the IP and portnumber they gave me as a server. Note the providername. I get this error when running enable-migrations in the console:
An error occurred accessing the database. This usually means that the connection to the database failed. Check that the connection string is correct and that the appropriate DbContext constructor is being used to specify it or find it in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=386386 for information on DbContext and connections. See the inner exception for details of the failure.
When I switch the providername to MySql.Data.MySqlClient, I get this error:
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.
What am I doing wrong? What's the simplest way to set up a connection to my MySQL database using EF6?
Edit:
My current web.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=x.x.x.x;Database=RademaekAuthentication;User=x;Password=x" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<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" />
</providers>

Inside of your web.config, the entire config is under the tag <configuration>. Anywhere inside of that you need to have
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<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" />
</providers>
</entityFramework>

I recently had this problem and solved it with the following connection string:
<add name="DefaultMySqlConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306; Initial Catalog=<name>; uid=<user>; pwd=<password>;" />
The Web.config also contains the following :
<entityFramework>
<defaultConnectionFactory
type="System.Data.Entity.Infrastructure.SqlConnectionFactory,
EntityFramework" />
<providers>
<provider
invariantName="MySql.Data.MySqlClient"
type="MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.Entity.EF6" />
</providers>
And in the system.data tag(create it right before the </configuration> tag if it doesn't exist), add the following code:
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" /><add name="MySQL" description="ADO.Net driver for MySQL" invariant="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/>
</DbProviderFactories>
This is my DbContext class:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class DatabaseContext : DbContext
{
public DatabaseContext(string connectionString)
: base(connectionString)
{ }
After this make sure to run the migrations and it hopefully works.
EDIT: Maybe it's worth mentioning I don't have the <providers> part in my config.

Related

"The provider did not return a ProviderManifestToken string" MySQL with Entity Framework

I have set up a new project in VS 2017. My intention is to use EF CodeFirst approach. So far I used Azure SQL Database, while in this test project I want to use my remote MySQL database. I have created the user and permissions are set just right. This remote database server is accessible to me over MySQL Workbench.
I have created a new blank MVC project and through Nuget I installed MySQL.Data.Entity (version 6.9.10).
My Context class:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class WebDb : DbContext
{
public WebDb() : base("WebDb")
{
}
public DbSet<Candidate> Candidates { get; set; }
}
My web.config has these entries:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<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, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<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.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
And my connection string is:
<connectionStrings>
<add name="WebDb" providerName="MySql.Data.MySqlClient" connectionString="server=x.x.x.x;uid=dbuser;pwd=password;database=temp1;" />
</connectionStrings>
I have a simple domain class
public class Candidate
{
public int Id { get; set; }
public string Name { get; set; }
}
When I give Enable-Migrations -Force command, I get
Checking if the context targets an existing database...
System.Data.Entity.Core.ProviderIncompatibleException: The provider
did not return a ProviderManifestToken string. --->
MySql.Data.MySqlClient.MySqlException: Unable to connect to any of the
specified MySQL hosts. at
MySql.Data.MySqlClient.NativeDriver.Open() at
MySql.Data.MySqlClient.Driver.Open() at
MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder
settings) at
MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection() at
MySql.Data.MySqlClient.MySqlPool.GetPooledConnection() at
MySql.Data.MySqlClient.MySqlPool.TryToGetDriver() at
MySql.Data.MySqlClient.MySqlPool.GetConnection() at
MySql.Data.MySqlClient.MySqlConnection.Open() at
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection
connection) at
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection
connection) --- End of inner exception stack trace --- at
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection
connection) at
MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection
connection) at
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection
connection, DbProviderManifest& providerManifest) at
System.Data.Entity.DbModelBuilder.Build(DbConnection
providerConnection) at
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext
internalContext) at
System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) at
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at
System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext
context, XmlWriter writer) at
System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter
w) at
System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action1
writeXml) at
System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext
context) at
System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration
configuration, DbContext usersContext, DatabaseExistenceState
existenceState, Boolean calledByCreateDatabase) at
System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration
configuration) at
System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration
migrationsConfiguration) at
System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
The provider did not return a ProviderManifestToken string.
I have searched far and wide to the best of my knowledge to no avail. What's happening here? What am I missing?
I've been having the same problems as you recently. Everything worked fine in SQL Server, but I was having lots of problems converting to MySQL. Some things that worked for me are:
Install-Package MySQL.Data -Version 6.9.9
Install-Package MySql.Data.Entity -Version 6.9.10
The newer 8.0 packages of MySQL appear to have problems. When I reverted to an older version, it worked.
Your app.config should look like:
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.9.9.0" newVersion="6.9.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<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, Version=6.8.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<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.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
</configuration>
Even after that, there are some discrepancies in MySQL that I'm just finding out. For example, your migration "index" statements will not work. You'll have to edit the migration file and build the index yourself. Another thing that I just read, but haven't encountered, is that the MySQL driver does not allow multiple connections which may mean changing the way you retrieve async collections. Lastly, I had a problem with RowVersion being a byte[]. I used a variation from the following article to solve this (hopefully!).
Better way to implement a row version with EF Core and MySQL?

MySQL version 7.0.6-IR3 issue when try to connect to db

I got below error when i try to connect to MySQL db.
Inheritance security rules violated by type:
'MySql.Data.MySqlClient.MySqlProviderServices'. Derived types must
either match the security accessibility of the base type or be less
accessible.
Below is the entityframework config:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=7.0.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
Before this i'm using MySQL 6.9.9 and no error happen.
Please help.
It is a known bug in MySql connector, tracked here:
https://bugs.mysql.com/bug.php?id=89134
Workaround is to downgrade to lower version connector (like the one you were using)

Entityframework wizard crashes Visual Studio 2013

I am trying to create an entityframework 6 model with a mysql database.
I am using the latest mysql server install and have added the Mysql.Data.Entity (24/02/2015) package to the project with nuget manager.
However when I go through the wizard (picking database first), I can connect to the database on the connection page but when I click next I first get a page stating that "an entity framework database provider compatible with this version could not be found for your data connection......"
If I then try to run the wizard again and click next on the connection page the wizard just disappears......
how do I get it to connect? I have always been able to do this without issue so I am baffled as to what is going on......
My App.Config is as follows
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<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, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</provider></providers>
</entityFramework>
<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.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>

MySql and Entity Framework 6

I am trying to use EF 6 with MySQL and am coming up with this error message going through the connection wizard:
Your project references the latest version of Entity Framework; however an Entity Framework database provider compatible with this version could not be found for your data connection.
Searching the internet it seems this is an issue plaguing people but I can't see that anyone has a solution.
I have references to EntityFramework 6.1.1 and MySql.Data 6.9.3, Mysql.Data.Entity.EF5 and MySql.Data.Entity.EF6 versions 6.8.3. I have also tried it with MySql.Data 6.8.3 but the same thing happens.
My web.config shows the following:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
Any thoughts or help would be gratefully received - otherwise I'll have to ditch MySql and go for Sql Server, putting a big hole in my schedule and an even bigger one in my wallet :-).
I have come across the post below, but it doesn;t seem to work for me:
Can't use a MySQL connection for entity framework 6
It looks to me like you have providers for both SQL Server and MySQL and the default connection factory is for the SQL Server provider. Do you need both? If not then you can remove the SQL Server parts altogether and make sure the default connection is MySQL.
Also, I notice that you have not posted the system.data portion of your config file. My working MySQL 6.9.3 config has this section.
My config looks like:
<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.9.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>

Cannot connect to SQL server AppHarbor

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Is the above relevant? I just have it in there in order not to screw things further.
But below is what I'm interested in, it is a connection string of my local sql server
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=localhost;Initial Catalog=TestDB;User ID=lews;Password='therin'" providerName="System.Data.SqlClient" />
"When you create a database on Sequelizer you can specify a connection string alias. This is done on the Sequelizer add-on page (follow the "Go to ..." link on the application overview). If you set this name as the name of your connection string in your configuration file, we'll automatically replace it with your Sequelizer connection string when your code is deployed."
That's a snippet from Appharbor's documentation. So I assume that Data Source, Initial Catalog, User ID and password is automatically replaced with the correct values by AppHarbor. But it can't connect for some reason.
Below is another string I am using with MySQL this time, again I assume that AppHarbor should automagically inject the right values, but the error it gives is:
"PeopleEntities cannot be found in the application configuration file"
What is going on?
<add name="PeopleEntities" connectionString="metadata=res://*/Context.People.csdl|res://*/Context.People.ssdl|res://*/Context.People.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;database=people"" providerName="System.Data.EntityClient" />
</connectionStrings>
Btw, the names "PeopleEntities" and "ApplicationServices" are used as aliases on AppHarbor.
And I have no idea how to use the code given in the documentation, databases is just not my thing.. how do I use both local and remote conn strings? Where in the code do I build the string and inject it? Do I have to do it whenever I create a DBContext instance? Etc..
Any ideas will be great, thanks!
EDIT:
Btw, if I hard code the connection strings, in the app.config and use a wcftestclient, it works, it queries the database.. but this isn't a good idea, apparently the connection strings can change without warning.
Anyway if I deploy it with the strings hardcoded and connect to the database with my site.. it doesn't query the SQL server.. really confused :(
http://support.appharbor.com/discussions/problems/2687-solved-mysql-provider-with-entity-framework-problem
For Entity Framework:
This piece of markup is unbelievable important:
<system.data>
<DbProviderFactories>
<clear />
<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.4.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
Oh make sure the MySQL connector assemblies are local. And it should be in your web.config file as well, to be extra sure place it in your WCF's App.config. Use the same addon alias in your connection string.
For SQL Server addon:
You need this markup in the web.config:
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
</system.web>
And you should be good to go..