'Unable to create a controller when using asp.net MVC 4/Entity Framework.

I am new to asp.net, and I am now facing a problem.

I have created a project using asp.net mvc4, and I have added an entity data model and successfully connected to MySQL. but when I tried to create a controller, I got the error message stated as below:

"unable to retrieve metadata for "client_test.client", Unable to find the requested .NET Framework data provider. it may not be installed."

from the "scaffolding options", I choose the template:

"MVC controller with read/write actions and views using Entity Framework"

and from the machine.config file, I could see MySQL data provider is there.

<DbProviderFactories>
  <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
  <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.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>

and the connectionString in the web.config is like this:

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-client_test-20130916144039;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-client_test-20130916144039.mdf" providerName="System.Data.SqlClient" />
<add name="Client_infoEntities" connectionString="metadata=res://*/clientDatabase.csdl|res://*/clientDatabase.ssdl|res://*/clientDatabase.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=127.0.0.1;user id=root;password=itplustest;persist security info=True;database=Client_info&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

and also I have added the code below to the web. config:

<system.data>
<DbProviderFactories>
  <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.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>

My connector/net version is 6.7.4.0 and i have MySQL for Visual Studio 1.0.2, I hope you guys could help me to solve my problem.

Thanks JY



Solution 1:[1]

I have been "playing" (In fact, it might be more appropriate to say "fighting") with this MySql thing with CodeFirst in Visual Studio. In fact it does work, but by difference with different readings found in googling around, I am still unable to have code first create its tables.

Anyway, I discovered you have to be very carefull about which version of Entity Framework you are using related to which version of MySql Connector.

I made a very simple console application, which is working fine after I manually created the table.

I am using EntityFramework 5.0 (current version) with the 6.7.4 (beta) version of the connector (Nuget Console PM> Install-Package MySql.Data.Entity -Pre) as this version works with EF 5, while the current version of the connector works with EF 4.2.

Here is my App config. Notice there is no (as in NO) reference to Entity Framework anywhere in this config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description="Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MyContext" connectionString="server=localhost;User Id=bernard;Password=some pwd; Persist Security Info=True;database=test" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
</configuration>

Hope this helps a bit. I am going to continue looking to see if Code First really works or not with MySql, as with MS SQL Tables and columns are created directly by code first if they don't exists.

EDIT A FEW HOURS LATER ** I finally was able to have code first create the table and columns, yepee! :-)

I explicitly called Database.SetInitializer, which I thought was not needed. Anyway, "REAL" Code First with MySql is now working just fine.

I wrote a blog post about this, and you can download the project from this post: http://www.bgsoftfactory.net/entity-framework-with-code-first-and-mysql-5-6-14-current-version/

Solution 2:[2]

Have you been able to create a data connection from Visual Studio/Server explorer ? I also see that your default connection point to LocalDB (MSSQL), which might be what you want, but I would keep it as simple as possible to start.

Have a look there, it may help you: http://blog.jongallant.com/2013/04/mysql-aspnet-mvc-entity-framework.html#.UjmwVMbKEqI

Regards, Bernard

Solution 3:[3]

I Had the same issue recently,My error was "There was an Error running the selected code generator:Unavble to retrive metadata for(my application)". I just copied and paste a working controller and change the inside of it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 BernardG
Solution 3 lojolis