1

I have ASP.NET MVC application solution split into 3 projects:

  • MyApp.Domain (EDMX File and Repositories)
  • MyApp.Test (Unit Tests)
  • MyApp.WebUI (MVC App)

When I get my application out of source control, set project MyApp.WebUI to the Startup project and F5 to run, I get an error from my DbContext...

An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code

Additional information: The Entity Framework provider type 'MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6' registered in the application config file for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

However if I right mouse click and 'Build' on my MyApp.Domain project first and then run it, my application makes a connection just fine.

Shouldn't I be able to just click run and it should build them all and just work?

Update 1

If I choose 'Rebuild all' or 'Build Solution' from the build menu, which I assume should rebuild all of my projects individually, it comes up with the error when I run it.

I have to actually choose 'Build' on my MyApp.Domain project within VS and then F5 my solution for my application to make a connection to the database without a DbContext error.

Update 2

If I right mouse click my EDMX file and 'Run Custom Tool' (as suggested in this Microsoft article to regenerate the EDMX code) and then F5 my solution to run, it still doesn't work. I have also tried opening the EDMX and saving it. It still comes up with the DbContext error.

Update 3 - App Config File

<?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=169433
  -->
<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>
  <connectionStrings>
    <add name="QuizEntities" connectionString="metadata=res://*/QuizModel.csdl|res://*/QuizModel.ssdl|res://*/QuizModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=uquiznew&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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>
  </entityFramework>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="owin:AppStartup" value="uQuiz.OwinStart" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <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="0.0.0.0-5.2.0.0" newVersion="5.2.0.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>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Luke
  • 22,826
  • 31
  • 110
  • 193
  • Maybe the `Domain` project appears to be out of date and, since it's a dependency, Visual Studio needs a "refresh". – Andrei V Sep 22 '14 at 09:04
  • 1
    I am getting my solution out of source control, I'm wondering if that might be affecting it..? When you say it's out of date, what do you mean, shouldn't it build all of my projects when I press F5? – Luke Sep 22 '14 at 09:05
  • it might be that the `edmx` build adds a generation step that is missed otherwise. – Davin Tryon Sep 22 '14 at 09:07
  • Are you the only one working on the project? Can the project be modified by another developer? I'm no expert on this matter, I'm just guessing here, but I think that `Run` just does a incremental build, at best. – Andrei V Sep 22 '14 at 09:07
  • Yeah, I'm the only one working on it, I have it in my own personal SVN repository :). – Luke Sep 22 '14 at 09:09
  • Even if I do 'Rebuild all' OR 'Build Solution' from the Build menu it doesn't work, I have to choose that specific project `MyApp.Domain` and Build... then the application works. It's really strange... – Luke Sep 22 '14 at 09:12
  • @DavinTryon Is there a way to force generation on build? – Luke Sep 22 '14 at 09:18
  • @DavinTryon Please see Update 2 – Luke Sep 22 '14 at 09:37
  • post your app config file – V2Solutions - MS Team Sep 22 '14 at 12:39
  • @V2Solutions-MSTeam I have now posted the web.config file – Luke Sep 22 '14 at 12:48

1 Answers1

1

As Tim.Tang states in their answer here:

In EF6, you need to use mysql connector 6.8.x, and add DbConfigurationTypeAttribute to you DbContext:

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DemoContext : DbContext{}

which MySqlEFConfiguration is in MySql.Data.Entity.EF6.dll in 6.8.x.

AppConfig:-

<?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>
  <connectionStrings>
    <add name="QuizEntities" connectionString="metadata=res://*/QuizModel.csdl|res://*/QuizModel.ssdl|res://*/QuizModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=uquiznew&quot;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <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.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
     </providers>
  </entityFramework>
</configuration>
Community
  • 1
  • 1
  • I can't believe that I missed this. Thank you for coming to my rescue! You have inadvertently sorted this question of mine http://stackoverflow.com/questions/25967745/c-sharp-mysql-with-entity-framework-error-cant-update-edmx-file I need to test to see if it will automatically build... – Luke Sep 22 '14 at 13:16
  • Sadly it didn't work for automatically building, I still have to rebuild my `MyApp.Domain` project to get it to work, but with the annotation I can update my database now! Yippeee! – Luke Sep 22 '14 at 13:23
  • 1
    If you copy the words of another, you must provide proper attribution, as I have done in the above. – Brad Larson Sep 23 '14 at 16:06