具有实体框架错误的C#Windows窗体应用程序:System.Data.Entity.Core.ProviderIncompatibleException

时间:2018-07-26 06:34:27

标签: c# windows forms entity-framework-6

我有一个C#Windows窗体程序,在其中使用EF连接到MYSQL。

当我尝试运行程序时,它给了我这个错误:

System.Data.Entity.Core.ProviderIncompatibleException:'访问数据库时发生错误。这通常意味着与数据库的连接失败。检查连接字符串是否正确,以及是否使用了适当的DbContext构造函数来指定它或在应用程序的配置文件中找到它。

这是我的DBContext类:

using System.Data.Entity;

namespace ADMMassUploader
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext() : base("MySqlConnection")
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        public DbSet<adm_main> AdmMain { get; set; }
    }
}

这是我的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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <appSettings>
    <add key="connectionstring" value="Server=localhost;Port=3306;Database=mysql;Uid=root;Pwd=xxxxx;default command timeout=0;SslMode=none" />
    <add key="tempFolder" value="C:\\MySqlBackup" />
    <add key="s3Folder" value="C:\\S3" />
    <add key="excludeSchema" value="'information_schema','mysql','sys','.serversidedebugger','sakila','world','performance_schema'" />
    <add key="excludeTablePrefix" value="and table_name not like 'temp%' and table_name not like 'sample%'" />
    <add key="fromEmail" value="colinc@rassure.net" />
    <add key="fromDisplayName" value="Rassure Admin" />
    <add key="recipients" value="colinc@rassure.net,Colin Chin" />
    <add key="testbackup" value="Y" />
    <add key="testsql" value="select distinct table_schema from information_schema.tables where table_schema in ('cx_acdm')" />
  </appSettings>
  <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.10.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider></providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.11.0" newVersion="8.0.11.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <connectionStrings>
    <add name="MySqlConnection" connectionString="Server=localhost;Database=dev_test;Uid=root;Pwd=ibm2007;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>  
</configuration>

这是我的按钮点击功能:

private void button1_Click(object sender, EventArgs e)
{
    using (var db = new ApplicationDbContext())
    {
        var query = db.AdmMain.Count();
        MessageBox.Show("Value=" + query);
    }
}

我在这里没有做什么吗?需要帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

设法通过此链接从MYSQL BUGS论坛中找到了一些东西

为了解决它,我不得不降级为:

MySql.Data-6.9.12(6.10.7和8.0.11出现了问题)

MySQL.Data.Entity-6.8.8(6.9.12和6.10.7出现了问题)

MYSQL BUGS

此外,我需要添加 “ [[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]”“ 到DBContext类:

using System.Data.Entity;

namespace ADMMassUploader
{
    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext() : base("MySqlConnection")
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        public DbSet<adm_main> AdmMain { get; set; }
    }
}