Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有MySql和EntityFramework的C#WPF_C#_Mysql_.net_Entity Framework - Fatal编程技术网

带有MySql和EntityFramework的C#WPF

带有MySql和EntityFramework的C#WPF,c#,mysql,.net,entity-framework,C#,Mysql,.net,Entity Framework,我试图用C#构建一个项目,使用WPF作为前端,EntityFramework/MySql作为后端,但我的代码甚至无法连接到数据库 我的项目结构如下: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.Conf

我试图用C#构建一个项目,使用WPF作为前端,EntityFramework/MySql作为后端,但我的代码甚至无法连接到数据库

我的项目结构如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    </configSections>
    <entityFramework>
        <providers>
            <provider invariantName="MySql.Data.MySqlClient"
              type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity"/>
        </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-6.9.8.0" newVersion="6.9.8.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <system.data>
        <DbProviderFactories>
            <remove invariant="MySql.Data.MySqlClient"></remove>
            <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.7.2.0"/>
        </DbProviderFactories>
    </system.data>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <connectionStrings>
        <add name="VolunteerDb" connectionString="server=localhost;user id=root;password=mypass;database=mydb" providerName="MySql.Data.MySqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.9.8.0" newVersion="6.9.8.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
  • 模型
  • 数据层框架(存储库接口、UnitOfWork接口等)
  • EntityFramework/MySql
  • 服务
  • UI(我希望我的连接字符串在这里)
  • 我从Oracle安装了.NET连接器,并尝试了一系列不同的Nuget软件包,但每当我的代码试图在EntityFramework/MySq层中执行任何操作时,我都会遇到一个异常

    目前,我已安装以下Nuget软件包:

  • 实体框架
  • MySql.Data
  • MySql.Data.Entity
  • my EntityFramework/MySq层中的app.config如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        </configSections>
        <entityFramework>
            <providers>
                <provider invariantName="MySql.Data.MySqlClient"
                  type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity"/>
            </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-6.9.8.0" newVersion="6.9.8.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
        <system.data>
            <DbProviderFactories>
                <remove invariant="MySql.Data.MySqlClient"></remove>
                <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.7.2.0"/>
            </DbProviderFactories>
        </system.data>
    </configuration>
    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <connectionStrings>
            <add name="VolunteerDb" connectionString="server=localhost;user id=root;password=mypass;database=mydb" providerName="MySql.Data.MySqlClient" />
        </connectionStrings>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.9.8.0" newVersion="6.9.8.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    
    
    
    我的UI层中的app.config如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
            <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        </configSections>
        <entityFramework>
            <providers>
                <provider invariantName="MySql.Data.MySqlClient"
                  type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity"/>
            </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-6.9.8.0" newVersion="6.9.8.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
        <system.data>
            <DbProviderFactories>
                <remove invariant="MySql.Data.MySqlClient"></remove>
                <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.7.2.0"/>
            </DbProviderFactories>
        </system.data>
    </configuration>
    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <connectionStrings>
            <add name="VolunteerDb" connectionString="server=localhost;user id=root;password=mypass;database=mydb" providerName="MySql.Data.MySqlClient" />
        </connectionStrings>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.9.8.0" newVersion="6.9.8.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    
    
    
    当我尝试运行并访问存储库中的数据库时,会出现以下异常:

    其他信息:无法加载文件或程序集的MySql.Data, 版本=6.9.8.0,区域性=中性,PublicKeyToken=c5687fc88969c44d'或 它的一个依赖项。找到的程序集的清单定义 与程序集引用不匹配

    要让EntityFramework使用MySql,我需要做什么?在此方面的任何帮助都将不胜感激

    编辑

    下面是packages.config的外观:

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="EntityFramework" version="6.1.3" targetFramework="net45" />
      <package id="Google.ProtocolBuffers" version="2.4.1.555" targetFramework="net45" />
      <package id="MySql.Data" version="7.0.2-DMR" targetFramework="net45" />
      <package id="MySql.Data.Entity" version="7.0.2-DMR" targetFramework="net45" />
    </packages>
    
    
    
    看起来您的包/引用的MySql版本可能不正确。尝试在包管理器控制台(工具>Nuget包管理器>包管理器控制台)中运行此命令:

    PS:您可能必须运行
    卸载程序包MySql-force

    看起来您的程序包/引用的MySql版本可能不正确。尝试在包管理器控制台(工具>Nuget包管理器>包管理器控制台)中运行此命令:


    PS:您可能必须运行
    卸载程序包MySql-force

    尝试将配置文件的行更改为:

    <bindingRedirect oldVersion="0.0.0.0-6.7.2.0" newVersion="6.7.2.0" />
    

    尝试将配置文件的行更改为:

    <bindingRedirect oldVersion="0.0.0.0-6.7.2.0" newVersion="6.7.2.0" />
    
    
    
    你能在问题中包含packages.config吗?你能在问题中包含packages.config吗?我以前见过类似的答案,但当我在EntityFramework/MySql项目上运行
    安装程序包MySql-version 6.9.8
    时,它给了我这个错误
    安装包:找不到包“MySql”的版本“6.9.8”
    @amura.cxg抱歉我忘了“.Data”请查看更新的答案尝试一下,这次安装了,但我仍然收到错误。我想知道我的
    app.config
    在尝试了这么多东西后是否格式不正确我以前也看到过类似的答案,但当我在EntityFramework/MySql项目上运行
    Install Package MySql-version 6.9.8
    时,它给了我这个错误
    安装包:找不到包“MySql”的版本“6.9.8”
    @amura.cxg抱歉我忘了“.Data”请查看更新的答案尝试一下,这次安装了,但我仍然收到错误。我想知道我的
    app.config
    在尝试了这么多东西之后是否出现了错误