Entity Framework type initializer exception

c# entity-framework visual-studio-2012

Question

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)

When I debug it from the network, VS reports: 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"}

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

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

1
8
4/17/2013 1:10:28 PM

Accepted Answer

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.

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.

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

20
4/17/2013 1:35:51 PM

Popular Answer

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

  1. Make sure connectionStrings element is after the configSections element.
  2. Make sure startup element is after the connectionStrings element.

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


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow