简体   繁体   中英

C# WPF with MySql and EntityFramework

I'm trying to build a project in C# using WPF as my front end and EntityFramework/MySql as my backed but I can't get my code to even make a connection to my DB.

The structure of my project is as follows:

  1. Models
  2. Data Layer Framework (Repository Interfaces, UnitOfWork interface, etc.)
  3. EntityFramework/MySql
  4. Services
  5. UI (I want my connection strings here)

I've installed the .NET connector from Oracle, and I've tried a bunch of different Nuget packages but whenever my code tries to do anything in the EntityFramework/MySq layer I get an exception.

Currently I have the following Nuget packages installed:

  1. Entity Framework
  2. MySql.Data
  3. MySql.Data.Entity

The app.config in my EntityFramework/MySq layer is as follows:

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

And the app.config in my UI layer is as follows:

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

And when I try to run and access the DB in my repository I get the following exception:

Additional information: Could not load file or assembly 'MySql.Data, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference

What do I need to do to get EntityFramework working with MySql? Any help on this would be appreciated!

Edit

Here's what the packages.config looks like:

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

It looks like you might have the wrong version of MySql your packages/references. Try running this in Package Manager Console (Tools > Nuget Package Manager > Package Manager Console):

 Uninstall-Package MySql.Data

 Install-Package MySql.Data -version 6.9.8

PS: you might have to run Uninstall-Package MySql -force

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM