Entity Framework type initializer exception

c# entity-framework visual-studio-2012

Question

On my computer, an entity framework project I'm working on runs smoothly. However, when it's run over the network, it crashes. The project recently underwent updates that included introducing the Dynamic Linq dll (System.Linq.Dynamic)

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

As an internal exception: "EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 or one of its dependencies could not be loaded. The manifest definition of the found assembly does not correspond to the assembly reference. EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 (Exception from HRESULT: 0x80131040) "}

I've tried the typical methods, such taking away thepackages Without success, I tried installing and removing the package from the project's root, then reinstalling it via the package manager interface.

My app's settings

<?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 please point out my mistakes?

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

Accepted Answer

Entity Framework 5.0 is listed in your App.config file, however certain projects in your code are still using Entity Framework 4.4 and expected to see it there.

What most likely occurred was as follows: Because you put EF 5.0 on a project that was built using.NET 4.0, the project now uses EF 4.4 rather than 5.0. (since 5.0 is only for .NET 4.5). You would still have EF 4.4 on that project if you later tried to upgrade it to.NET 4.5. To have the correct reference to EF 5.0 in that project, you would need to reinstall EF once again.

Try it out and let me know how it goes.

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

Popular Answer

Check the following in the App.config file after installing the correct version of EF on all projects.

  1. A connectionStrings element should come after a configSections element.
  2. A startup element should come after a 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