0

Unable to Connect to MySQL in ASP.NET MVC using Entity Framework 6.0.0.0

a few seconds ago|LINK

Hi,

I have tried all night to connect my ASP.NET MVC project to MySQL database but am unsuccessful. I found code to connect to a MySQL database (using MVC 4) on this link

on stackoverflow. However I am using MVC 5 and EF 6 to connect and it seems something is amiss. When accessing the database, an exception is thrown

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code Additional information: 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

I connected to a MySql database I had created and used the connectionstring on the web.config file. I set up the web.config as specified by the link I have provided. However I didnt set up Simple membership since I don't need it currently. I was able to create a controller with read write actions using EF on the same MySQL database without any problem. I have included below my whole web.config (the one on the project's root) below.

        <?xml version="1.0" encoding="utf-8"?>
    <!--
      For more information on how to configure your ASP.NET application, please visit
      http://go.microsoft.com/fwlink/?LinkId=301880
      -->
    <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" />
    <!--<section name="entityFranework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />-->
  </configSections>
  <!--<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=&quot;|DataDirectory|\aspnet-Automation Of Services-20150105091844.mdf&quot;;Initial Catalog=&quot;aspnet-Automation Of Services-20150105091844&quot;;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>-->
    <connectionStrings>
    <add name="ChangeRequestsContext" connectionString="server=localhost;user id=root;persistsecurityinfo=True;database=changerequests" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
   </appSettings>
   <system.web>
    <authentication mode="None" />
     <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    </system.web>
    <system.webServer>
      <modules>
        <remove name="FormsAuthentication" />
     </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
       <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
       </dependentAssembly>
        <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
   </runtime>
  <!--<configSections>
 <section name="entityFranework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />    
  </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.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
        </provider></providers>-->
        <contexts>
        <context type="Automation_Of_Services.Models.ChangeRequestsContext, AutomationOfServices">
        <databaseInitializer type="Automation_Of_Services.EntityFramework.MySql.DropCreateMySqlDatabaseIfModelChanges,  AutomationOfServices">
       </databaseInitializer>
      </context>
    </contexts>
    <defaultConnectionFactory type="MySql.Data.MySqlClient.MySqlClientFactory,MySql.Data" />
  </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.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
    <!--<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" />
     </DbProviderFactories>-->
    </system.data>
   </configuration>
Dev
  • 1,146
  • 2
  • 19
  • 30

1 Answers1

0

You provider name for mysql seems not correct:

providerName="MySql.Data.MySqlClient"

You should find the right provider. I suggest getting official provider from mysql website. They have provider for EF 6.0 too.

qamar
  • 1,437
  • 1
  • 9
  • 12
  • I cannot see on my web.config where I wrote the name of the provider wrongly. Kindly point out so I can change. The only place I find providerName I see I have provided "MySql.Data.MySqlClient". Thanks for the quick response – Dev Jan 06 '15 at 04:04
  • I have copy pasted the code you provided but the Exception is still thrown. I can see some info from the Exception thrown that states **"Make sure the provider is registered in the 'entityFramework' section of the application"** Could this be the problem? – Dev Jan 06 '15 at 04:19
  • No. You need to update following with the right Mysql provider for entity framework. providerName="MySql.Data.MySqlClient". Your provider name is not correct. You need to have mysql connector for .net. Here is where you can download that : http://dev.mysql.com/downloads/connector/net/ Once you have downloaded then use the dll from this conenctor to connect to database – qamar Jan 06 '15 at 04:22
  • I have downloaded and installed the latest connector. Sorry that I don't still get you. After throwing an Exception again after I installed latest connector, I changed web.config such that every instance I found MySql.Data, I appended text to it so that all of them read MySql.Data.MySqlClient. Now I have a new ConfigurationException. Details are An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Data.dll but was not handled in user code Additional information: Failed to find or load the registered .Net Framework Data Provider. – Dev Jan 06 '15 at 04:39
  • Please explain step by step what I should change to get my project working. Thanks in advance. – Dev Jan 06 '15 at 04:40
  • I got past this a little while ago. Have another issue, but you can see my web.config stuff here http://stackoverflow.com/questions/27827085/how-do-i-fix-specified-key-was-too-long-max-key-length-is-1000-bytes?noredirect=1#comment44060286_27827085 – Fred Johnson Jan 07 '15 at 19:40
  • If you have fixed your issue with help of this answer you must mark it as answer or if you have solved by yourself you should write that answer too so that everybody is benefited. Regarding other issue I will take a look. – qamar Jan 08 '15 at 03:16