MVC4 + Entity Framework 4.4 + MySql + POCO/Code First
I\'m setting up the above configuration .. here are my classes:
namespace BTD.
I tested also around this bug and saw an other problem.
Following code is in my Web.config (without, it don't work):
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
Changed it to:
<entityFramework>
Works for me... add the scaffolding and then change it back
The imesh suggesting almost solve my problem, but additionally I temporary commented line
[DbConfigurationType(typeof(MySqlEFConfiguration))]
which was in DBContext class. And of course after creation controller this line should be uncommented and change back System.Data.SqlClient to MySql.Data.MySqlClient in config file.
Using VS 2013, MySQL Connector/NET 6.9.6, Entity Framework 6, Web API 2.2 and MySQL server 5.7 I had to combine the answers to prevent the error about "Unable to retrieve metadata".
To successfully add a controller to a .NET project that uses a MySQL connection, do the following:
System.Data.SqlClient as the providerName, and comment the one for MySQL. It doesn't matter whether the connection string is valid.MySqlEFConfiguration isn't enabled in any way.About the second point, the MySQL documentation on using Connector/NET with EF6 states three possible ways to enable the MySqlEFConfiguration. Ensure that none of these are enabled while adding controllers using the VS template.
- Adding the DbConfigurationTypeAttribute on the context class:
[DbConfigurationType(typeof(MySqlEFConfiguration))]
Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup
Set the DbConfiguration type in the configuration file:
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
It seems that MVC4 Controller scaffolding is not properly recognizing MySql Connection String. Change the connection string as shown below when generating EF CRUD code for Controllers:
<connectionStrings>
<add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="System.Data.SqlClient" />
</connectionStrings>
Change it back to standard when running the application:
<connectionStrings>
<add name="BTDContext" connectionString="Data Source=host_name;Database=database_name;uid=user_id;pwd=password;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Note the change, provider name.
Please try using the
System.ComponentModel.DataAnnotations
namespace along with the [Key] attribute on the EDM class members.
It worked for me.
I've been having the same problem using EF 4.3.1 and MySql Connecter/Net 6.6.4.0
This worked for me, no need to connect to a different db or extra code in the Context class.
Add this between the entityFramework tags in your web.config file when you want to build a scaffold:
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
Then comment the above code out when you want to run migrations and vice versa.
So you web.config will look like so:
<entityFramework>
<contexts>
<context type="ApptManager.Models.AppointmentsManagerContext, ApptManager">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[ApptManager.Models.AppointmentsManagerContext, ApptManager], [ApptManager.Migrations.Configuration, ApptManager]], EntityFramework" />
</context>
</contexts>
<!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>-->
</entityFramework>
This is quite ridiculous how .NET developers have to jump through some arduous hoops in order to get their code working.
Working example
https://github.com/dublinan/mvc-mysql-ef-example
Links to my github project