"Exception has been thrown by target of invocation" when creating controller (ASP.NET MVC 5 with MySql)

asp.net-mvc-5 c# entity-framework-6 mysql

Question

I'm setting up a solution containing two projects: 1) my class library and 2) my web UI. Ultimately the website will be used to show query results from an existing mysql database.

I struggled to create the ado.net entity model for a while. But I thought I had figured it out after updating the mysql connector for visual studio and following the instructions here: https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.html

Now that the entity model is created I am struggling to create a simple controller with scaffolding. I've double checked my connection string and ef provider in web.config and app.config.

I've looked through these questions but have not found my answer:

Exception has been thrown by the target of an invocation- while creating the controller

Application can't scaffold items

Enity Framework With MySQL

I'm pretty sure the issue is in how I have configured it. For a while I thought that what is in app.config didn't matter because the UI project (containing web.config) was set as the startup project. Now I'm just confused. Which is why I have included both below. Any insight is greatly appreciated.

From web.config:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MySolution.UI-20161111094519.mdf;Initial Catalog=aspnet-MySolution.UI-20161111094519;Integrated Security=True"
      providerName="System.Data.SqlClient" />
    <add name="MySolutionEntities" connectionString="metadata=res://*/Entities.DevData.csdl|res://*/Entities.DevData.ssdl|res://*/Entities.DevData.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=****;user id=****;password=****;database=mydatabase&quot;"
      providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="System.Data.MySqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFrameworkSqlServer" />
    </providers>
  </entityFramework>

From app.config:

<?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>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" 
                type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    <provider invariantName="MySql.Data.SqlClient" 
              type="MySql.Data.Entity.SqlServer.ProviderServices, EntityFramework.SqlServer"/></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.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MySolutionEntities" connectionString="metadata=res://*/Entities.DevData.csdl|res://*/Entities.DevData.ssdl|res://*/Entities.DevData.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=****;user id=****;password=****;database=mydatabase&quot;" 
         providerName="System.Data.MySqlClient" />
  </connectionStrings>
</configuration>
1
0
5/23/2017 10:32:37 AM

Popular Answer

Late answer but I hope it helps somebody I had same problem, I just solve it by uninstalling and reinstalling entity-framework through the nuget package manager console. o Run Package Manager Console :

Tools -> NuGet Package Manager -> Package Manager Console

To Remove:

UnInstall-Package EntityFramework

To Reinstall:

Install-Package EntityFramework

I hope this works for you as well

0
8/29/2017 9:53:39 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow