c# - Entity Framework 类型初始值设定项异常

标签 c# entity-framework visual-studio-2012

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

当我从网络调试它时,VS 报告:'System.Data.Entity.Internal.AppConfig' 的类型初始值设定项引发异常

内部异常是:“无法加载文件或程序集‘EntityFramework,Version=4.4.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089’或其依赖项之一。找到的程序集的 list 定义与程序集引用不匹配。 (HRESULT 异常:0x80131040)":"EntityFramework,版本=4.4.0.0,文化=中性,PublicKeyToken=b77a5c561934e089"

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

我的应用程序配置:

<?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>

谁能指出我做错了什么?

最佳答案

您的 App.config 文件列出了 Entity Framework 5.0,并且您代码中的某些项目仍在使用 EF 4.4 并期望在 App.config 文件中找到它。

这是最有可能发生的情况:您在一个在 .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。

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

关于c# - Entity Framework 类型初始值设定项异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16060972/

相关文章:

c++ - 当我们在使用 VS 的 C++ 项目中将库作为附加依赖项引用时,到底发生了什么?

c# - 为什么用户控制类访问对另一个线程不安全?

c# - Mysql数据库列数的图表

entity-framework - Entity Framework 6.1.2 中有哪些新增功能?

c# - 如何在 LINQ-to-Entities 查询中使用自定义属性?

c# - 如何更改 Vs2012 中的异常消息?

c# - Windows 8 应用程序的打开文件对话框

c# - 用户键入时无法显示正确的最大值 (1 2 3 4 5 6 7 8 9 10)

c# - 在 Metro 中使用 NHibernate

c# - 创建一个平台来处理未知实体的 CRUD 场景