简体   繁体   English

'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' 的类型初始值设定项抛出异常

[英]The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception

I have ASP.Net MVC site.我有ASP.Net MVC站点。

Technology Stack技术栈

  • ASP.Net 4.6 ASP.Net 4.6
  • C#.Net C#.网
  • EF 6英孚 6
  • MySQL -Database MySQL-数据库

While I am trying to generate the database using Nuget command:-当我尝试使用Nuget命令生成数据库时:-

Enable-Migrations -force

I am getting the below exception我收到以下异常

The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception. “System.Data.Entity.Migrations.DbMigrationsConfiguration`1”的类型初始值设定项引发异常。

Following things are already cross checked & tried by me:-我已经交叉检查并尝试了以下内容:-

My App.Config:-我的 App.Config:-

 <connectionStrings>
<add name="mydbContext" providerName="MySql.Data.MySqlClient" connectionString="Server=localhost;port=8080;database=mydb;uid=root;password=" />
</connectionStrings>



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

Have fixed it by making the configSections the first child of configuration in the config file.通过使configSections成为配置文件中配置的第一个子项来修复它。

<configuration> 

... ... ……

在实体框架中The type initializer错误很可能是由于Web.config文件格式错误,例如有两个或多个连接字符串部分,或者连接字符串本身存在问题,例如无效的提供程序。

Just add configSections inside the configuration section if not added.如果没有添加,只需在配置部分中添加configSections And configSections must be first child of configuration section just like below code.并且configSections 必须是配置部分的第一个子项,就像下面的代码一样。

 <configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="ConnectionString" connectionString="data source=20.20.1.1;initial catalog=myDB;user id=sa;password=123456;persist security info=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/> </connectionStrings> .... <!-- other configuration --> .... </configuration>

The type initialization error arises because the invariantName=MySql.Data.MySqlClient appears twice in the <providers> section of the web.config file.出现类型初始化错误是因为invariantName=MySql.Data.MySqlClientweb.config文件的<providers>部分中出现了两次。

Removing one of them (by commenting it out) solves the issue:删除其中一个(通过注释掉)可以解决这个问题:

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
        <!--<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
        <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.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
  </providers>
</entityFramework>

Even I got this error while migrating.甚至我在迁移时也遇到了这个错误。 I had a data access layer(DAL) having my context class and web application referencing the data access layer.我有一个数据访问层(DAL),其中包含引用数据访问层的上下文类和 Web 应用程序。 To migrate you have to add the connection string in the app.congig file of the DAL and also the web.config file in your web application.要进行迁移,您必须在 DAL 的 app.congig 文件以及 Web 应用程序中的 web.config 文件中添加连接字符串。 On removing the default connection string from the web.config file it worked for me.从 web.config 文件中删除默认连接字符串时,它对我有用。 Replace this替换这个

<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\aspnet-foo-20190108014329.mdf;Initial Catalog=aspnet-foo-20190138014529;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>

by

<connectionStrings> <add name="foo" connectionString="Data Source=foo; Initial Catalog=foo; Integrated Security=True; MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/> </connectionStrings>

This worked for me.这对我有用。

I found a solution to this problem.我找到了解决这个问题的方法。

My app.config was missing a section name in configSections:我的app.configconfigSections:缺少部分名称configSections:

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

I simply added it and it worked.我只是添加了它并且它起作用了。

This configuration worked for me:这个配置对我有用:

<?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" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <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.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <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.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="MyLocalDatabase" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=mycontext;uid=root;password=********" />
  </connectionStrings>
</configuration>

I was getting errors similar to yours because of a wrong installation of the MySql connector client in my development machine.由于在我的开发机器中错误安装了MySql连接器客户端,我收到了与您类似的错误。 I installed MySql connector Nuget packages within Visual Studio, but it was not enough.我在 Visual Studio 中安装了MySql连接器 Nuget 包,但这还不够。 I also needed to install MySql client which I downloaded from MySql website.我还需要安装从MySql网站下载的MySql客户端。 After that the Type errors went away.之后, Type错误消失了。

These are my Nuget packages:这些是我的 Nuget 包: 在此处输入图片说明

And this is the download page of MySql connector which I downloaded and installed manually.这是我手动下载并安装的 MySql 连接器的下载页面

Note: the defaultConnectionFactory value is never used if you specify explicitly the type of your provider.注意:如果您明确指定提供者的类型,则从不使用defaultConnectionFactory值。 In that case it doesn't matter which value you have in the configuration file in this setting.在这种情况下,您在此设置的配置文件中拥有哪个值并不重要。 See point 3 in this reference doc page in MySql website .请参阅MySql 网站此参考文档页面中的第3 点。

I recommend you to check if your MySql is correctly installed in your machine by using MySql command line client console.我建议您使用 MySql 命令行客户端控制台检查您的MySql是否正确安装在您的机器上。 Try to connect to the server from outside Visual Studio with your connection string values.尝试使用连接字符串值从 Visual Studio 外部连接到服务器。

Remove the connection string and add the migration again.删除连接字符串并再次添加迁移。 Connection string will be added automatically.连接字符串将自动添加。 I am having the same issue and it worked for me.我有同样的问题,它对我有用。

When I had this problem I realised that entityframework was not declared in config file.当我遇到这个问题时,我意识到实体框架没有在配置文件中声明。

So I added所以我加了

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

above the connection string在连接字符串上方

<connectionStrings> 
  <add name="DefaultConnection" connectionString="Server=DESKTOP-ABHI;Initial Catalog=IdentityDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

First check the connection string in the web.config file.首先检查 web.config 文件中的连接字符串。

If you have two connection strings then comment the second connection string.如果您有两个连接字符串,则注释第二个连接字符串。

I had these same issue... Check Root Web.config (Fixed)我遇到了同样的问题...检查 Root Web.config(已修复)

  • 1- Check the configSections is first child 1-检查 configSections 是第一个孩子
  • 2 Remove the Extra connection strings you have (Comment them out) Stay with the one you want. 2删除您拥有的额外连接字符串(将它们注释掉)保留您想要的那个。

Note: I was reading leeNaylors book(asp.net mvc with entity f...) according to his book this should´not be an issue, having more than 1 Connection string... but it turns out it does matter for migrations.注意:我正在阅读 leeNaylors 的书(asp.net mvc with entity f...),根据他的书,这应该不是问题,有超过 1 个连接字符串...但事实证明它对迁移很重要。 [web.config][image] [web.config][图片]

Thumbs up if my answered helped you...如果我的回答对你有帮助,请点个赞。。。

Web.config image Web.config 图片

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;System.Data.Entity.Migrations.DbMigrationsConfiguration`1&#39;的类型初始值设定项引发了Oracle数据库异常 - The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration`1' threw an exception with Oracle Database System.Data.Entity.MigrateDatabaseToLatestVersion 的类型初始值设定项引发异常 - The type initializer for System.Data.Entity.MigrateDatabaseToLatestVersion threw an exception &#39;System.Data.Entity.Internal.AppConfig&#39;的类型初始值设定项引发了Windows XP的异常 - the type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception for Windows XP SQL Server Express “'system.data.entity.internal.appconfig' 的类型初始化程序引发异常”错误 - SQL Server Express “the type initializer for 'system.data.entity.internal.appconfig' threw an exception” error &#39;System.Data.Entity.Internal.AppConfig&#39;的类型初始化程序在子网站上引发了一个例外 - The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website 未处理typeinitializationexception。&#39;System.Data.Entity.Internal.AppConfig&#39;的类型初始值设定项引发了异常 - typeinitializationexception was unhandled .The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception System.Data.SqlClient的类型初始值设定项引发了异常 - type initializer for System.Data.SqlClient threw an exception 在 .NET4.5 上安装 EntityFramework5.0.0 后错误:“System.Data.Entity.Internal.AppConfig”的类型初始化程序在运行时引发异常 - After installing EntityFramework5.0.0 on .NET4.5 Error: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception at Runtime 类型初始值设定项引发异常 - Type initializer threw an exception &#39;&#39;的类型初始值设定项引发了异常 - The type initializer for '' threw an exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM