C# 实体框架类型初始值设定项异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16060972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:52:58  来源:igfitidea点击:

Entity Framework type initializer exception

c#entity-frameworkvisual-studio-2012

提问by mcalex

I have an entity framework project that is working fine on my machine, but falls over when run from the network. Recent changes to the project include adding the Dynamic Linq dll (System.Linq.Dynamic)

我有一个实体框架项目,它在我的机器上运行良好,但从网络运行时失败了。最近对项目的更改包括添加动态 Linq dll (System.Linq.Dynamic)

When I debug it from the network, VS reports: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

当我从网络调试它时,VS 报告: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception

The inner exception is: "Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"}

内部异常是:“无法加载文件或程序集 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(异常来自HRESULT: 0x80131040)":"EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"}

I have tried the usual tricks: removing the packagesdirectory from the root of the project, uninstalling and reinstalling from the package manager console, but to no avail.

我尝试了通常的技巧:packages从项目的根目录中删除目录,从包管理器控制台卸载并重新安装,但无济于事。

My app.config:

我的 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="LGFinance.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="LGFinanceEntities" connectionString="metadata=res://*/Model.LGFinanceContext.csdl|res://*/Model.LGFinanceContext.ssdl|res://*/Model.LGFinanceContext.msl;provider=System.Data.SqlClient; provider connection string='data source=lightning;initial catalog=DLGDB;Integrated Security=true;Password=******;multipleactiveresultsets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <applicationSettings>
    <LGFinance.Properties.Settings>
      <setting name="Setting" serializeAs="String">
        <value />
      </setting>
    </LGFinance.Properties.Settings>
  </applicationSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Windows.Interactivity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Can someone point out what I've done wrong?

有人能指出我做错了什么吗?

采纳答案by Corey Adler

Your App.config file has Entity Framework 5.0 listed, and some project in your code is still holding onto EF 4.4 and expecting to find it in the App.config file.

您的 App.config 文件已列出 Entity Framework 5.0,并且您代码中的某些项目仍保留 EF 4.4,并希望在 App.config 文件中找到它。

Here's what most likely happened: You installed EF 5.0 on a project that was building in .NET 4.0, which makes the version of EF 4.4 instead of 5.0 (since 5.0 is only for .NET 4.5). If you tried to up the project to .NET 4.5 later on you'll still have EF 4.4 on that project. That would require you to reinstall EF again on that project to have the correct reference to EF 5.0.

以下是最有可能发生的情况:您在以 .NET 4.0 构建的项目上安装了 EF 5.0,这使得 EF 4.4 的版本不是 5.0(因为 5.0 仅适用于 .NET 4.5)。如果您稍后尝试将项目升级到 .NET 4.5,那么该项目中仍然会使用 EF 4.4。这将需要您在该项目上再次重新安装 EF 才能正确引用 EF 5.0。

Give that a shot and let me know if it works.

试一试,让我知道它是否有效。

回答by Diganta Kumar

All project have to have correct version of EF installed first and then check the following in the App.config file,

所有项目都必须先安装正确版本的 EF,然后在 App.config 文件中检查以下内容,

  1. Make sure connectionStringselement is after the configSectionselement.
  2. Make sure startupelement is after the connectionStringselement.
  1. 确保connectionStrings元素在configSections元素之后。
  2. 确保启动元素在connectionStrings元素之后。

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <configSections>
         <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </configSections>
   <connectionStrings>
         <add name="SchedulingContext" connectionString="Data Source=XXX\SQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX"   providerName="System.Data.SqlClient"/>
   </connectionStrings>
   <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>       
 </configuration>

回答by Mata

I could fix this problem installing: Entity Framework 6 Tools for Visual Studio 2012 & 2013 - http://www.microsoft.com/en-gb/download/confirmation.aspx?id=40762

我可以通过安装来解决这个问题:Entity Framework 6 Tools for Visual Studio 2012 & 2013 - http://www.microsoft.com/en-gb/download/confirmation.aspx?id=40762

Regards, AM

问候,上午

回答by Navan

I tried all, after that just removed the following providers, it worked for me

我尝试了所有,之后只是删除了以下提供者,它对我有用

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>