Problem to execute "Update-Database"

1

I'm following the tutorial for this video to set up an ASP.NET MVC project using EntityFramework6. The database used in the tutorial is SQLServer and what I'm trying to use is MySQL.

I already have the MySQL Connector installed.

I was able to run the "Enable-Migrations" command (at 39 min of the video) but I'm having trouble executing "Update-Database -Verbose".

The error "header" follows:

update-database -verbose Using StartUp project 'ProjetoModeloDDD.MVC'. Using NuGet project 'ProjetoModeloDDD.Infra.Data'. Specify the '-Verbose' flag to view the SQL statements being applied to the target database. System.TypeInitializationException: The type initializer for 'ProjetoModeloDDD.Infra.Data.Contexto.ProjetoModeloContext' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

The only reference I found for "MySql.Data" is in App.Data which looks like it is in version 6.9.6.0, as well as assembly :

<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.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" /> </DbProviderFactories>

Theprojectrepositoryfollows: link

Grateful if anyone can help.

    
asked by anonymous 09.07.2015 / 04:09

2 answers

3

There are several things wrong with your setup. Before switching the technology from the bank, you should have followed the tutorial to the end. Come on.

First, in your App.config :

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

This:

type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"

It is from SQL Server, not from MySQL. The correct one is, in addition to changing the factory , specify a non-default configuration (or the Entity Framework will also try to use the SQL Server configuration) . Therefore:

Another thing is the provider configuration:

    <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
    </providers>

You need to set the assembly version here, or you may run the wrong MySQL version yourself.

<providers>
  <provider invariantName="MySql.Data.MySqlClient"
            type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</providers>

Still, the MySQL team itself makes a lot of mess with the package. So much so that you added a discontinued version of the feature library. In its packages.config :

<package id="MySQL.Data.Entities" version="6.8.3.0" targetFramework="net45" />

Do not use this package MySQL.Data.Entities . Swap by MySQL.Data.Entity , in the singular.

<package id="MySQL.Data.Entity" version="6.9.6" targetFramework="net45" />

I made some more updates to your project. It's all in GitHub's pull request .

    
09.07.2015 / 06:26
1

With the help of the Gypsy the problem has been solved. I leave here what was modified in the project, based on the previous response and the changes it made.

App.config (in Data.Data)

The tag was added with the addition of the codeConfigurationType attribute and change from the <defaultConnectionFactory> tag to the correct MySql tag.

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />

The provider version was also added.

<provider invariantName="MySql.Data.MySqlClient"
        type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
Package.csproj (in Infra.Data)

The package configuration is as follows:

<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
<package id="MySql.Data" version="6.9.6" targetFramework="net45" /
<package id="MySQL.Data.Entity" version="6.9.6" targetFramework="net45" />

In addition, the same changes in App.config were made in Web.config (in the MVC layer) and so I could execute the command Update-Database .

    
12.07.2015 / 20:55