huseyinm88
huseyinm88

Reputation: 13

Entity Code First Error (Mysql)

When I run "Enable-Migrations -Force" command on my Class Library project, I see following error.

Note: Mysql.Data and Mysql.Data.Entity has been installed.

System.TypeInitializationException: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. ---> System.TypeLoadException: Inheritance security rules violated by type: 'MySql.Data.Entity.MySqlEFConfiguration'. Derived types must either match the security accessibility of the base type or be less accessible.

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>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Server=10.10.10.10;Database=dbName;Uid=user;Pwd=p;" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <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.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider></providers>
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

DbContext.cs

[DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MyDbContext : DbContext
    {
        public MyDbContext() : base("DefaultConnection")
        {

        }

        public DbSet<User> Users { get; set; }
        public DbSet<Board> Boards { get; set; }


    }

Upvotes: 1

Views: 3498

Answers (1)

Ronaldo Ribeiro
Ronaldo Ribeiro

Reputation: 86

I'm having exactly the same problem in both VS 2015 and VS 2017, have tried everything and nothing works :(

--- Edit

I get the job done after downgrade the MySQL.Data to 6.8.8.0 . Worked both VS 2015 and VS 2017.

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class Context : DbContext
{
    public Context() : base("MyContext")
    {

    }

    public DbSet<Foo> foo;

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Foo>();
    }
}

Upvotes: 7

Related Questions