当我在类库项目上运行“Enable-Migrations -Force”命令时,我看到以下错误。
注意:已安装Mysql.Data和Mysql.Data.Entity。
System.TypeInitializationException:类型初始值设定项 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1'扔了一个 例外。 ---> System.TypeLoadException:继承安全性规则 违反了类型:'MySql.Data.Entity.MySqlEFConfiguration'。派生 类型必须匹配基类型的安全可访问性或 不太容易接近。
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; }
}
答案 0 :(得分:7)
我在VS 2015和VS 2017都遇到了完全相同的问题,尝试了一切,没有任何作用:(
---编辑
我将MySQL.Data降级到6.8.8.0后完成了工作。同时参与VS 2015和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>();
}
}