Problem to run "Update-Database"

Asked

Viewed 858 times

1

I’m following the tutorial of 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 Mysql Connector installed.

I managed to execute the command "Enable-Migrations" (at 39 min. of the video) but I am having trouble executing "Update-Database -Verbose".

Follows the "header" of the error:

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 to "Mysql.Data" is in App.Data which appears to be 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>

inserir a descrição da imagem aqui

Follow the project repository: https://github.com/feliupe/ProjetoDDD

Grateful if anyone can help.

2 answers

3


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

First, in your file 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 SQL Server, not Mysql. The correct is, in addition to changing the Factory, specify a non-standard configuration (or the Entity Framework will also try to use the SQL Server configuration). Therefore:

Another thing is the configuration of preview:

    <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 run the risk of Mysql itself using the wrong version.

<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 confusion with the package. So much so that you added a discontinued version of the entity library. In your packages.config:

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

Don’t use this package MySQL.Data.Entities. Exchange for MySQL.Data.Entity, in the singular.

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

I’ve made a few more updates to your project. It’s all on pull request github.

1

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

App.config (under Data)

Preview tag with attribute addition codeConfigurationType and tag change <defaultConnectionFactory> to the correct, Mysql.

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

The Provider version has also been 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 was 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" />

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.