【问题标题】:Unable to Connect to MySQL in ASP.NET MVC using Entity Framework 6.0.0.0无法使用 Entity Framework 6.0.0.0 在 ASP.NET MVC 中连接到 MySQL
【发布时间】:2015-03-03 17:11:15
【问题描述】:

无法使用 Entity Framework 6.0.0.0 在 ASP.NET MVC 中连接到 MySQL

几秒钟前|LINK

嗨,

我整晚都在尝试将我的 ASP.NET MVC 项目连接到 MySQL 数据库,但没有成功。我在此链接上找到了连接到 MySQL 数据库(使用 MVC 4)的代码

关于堆栈溢出。但是,我使用 MVC 5 和 EF 6 进行连接,似乎有些不对劲。访问数据库时抛出异常

EntityFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 附加信息:没有为具有不变名称“MySql.Data.MySqlClient”的 ADO.NET 提供程序找到实体框架提供程序。确保提供程序已在应用程序的“entityFramework”部分注册

我连接到我创建的 MySql 数据库,并在 web.config 文件中使用了连接字符串。我按照我提供的链接指定的方式设置了 web.config。但是我没有设置简单会员,因为我目前不需要它。 我能够在同一个 MySQL 数据库上使用 EF 创建具有读写操作的控制器,没有任何问题。我在下面包含了我的整个 web.config(项目根目录中的那个)。

        <?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>

【问题讨论】:

    标签: mysql asp.net asp.net-mvc entity-framework-6 c#-5.0


    【解决方案1】:

    您的 mysql 提供者名称似乎不正确:

    providerName="MySql.Data.MySqlClient"
    

    您应该找到合适的提供商。我建议从 mysql 网站获取官方提供者。他们也有 EF 6.0 的提供者。

    【讨论】:

    • 我在 web.config 上看不到我错误地写了提供者名称的地方。请指出,以便我可以更改。我找到providerName的唯一地方我看到我提供了“MySql.Data.MySqlClient”。感谢您的快速响应
    • 我已经复制粘贴了您提供的代码,但仍然抛出异常。我可以从抛出的异常中看到一些信息,指出 “确保提供程序已在应用程序的 'entityFramework' 部分中注册” 这可能是问题吗?
    • 没有。您需要为实体框架使用正确的 Mysql 提供程序更新以下内容。 providerName="MySql.Data.MySqlClient"。您的提供商名称不正确。你需要有 .net 的 mysql 连接器。您可以在此处下载:dev.mysql.com/downloads/connector/net 下载后,使用此连接器中的 dll 连接到数据库
    • 我已经下载并安装了最新的连接器。对不起,我仍然没有得到你。在我安装最新的连接器后再次抛出异常后,我更改了 web.config,以便我找到 MySql.Data 的每个实例,我将文本附加到它,以便所有实例都读取 MySql.Data.MySqlClient。现在我有一个新的 ConfigurationException。详细信息是 System.Data.dll 中发生“System.Configuration.ConfigurationErrorsException”类型的异常,但未在用户代码中处理其他信息:无法找到或加载注册的 .Net Framework 数据提供程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2019-09-16
    相关资源
    最近更新 更多