mcalex
mcalex

Reputation: 6778

Entity Framework type initializer exception

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?

Upvotes: 8

Views: 49585

Answers (4)

Navan
Navan

Reputation: 49

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>

Upvotes: 4

Mata
Mata

Reputation: 21

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

Regards, AM

Upvotes: 0

Diganta Kumar
Diganta Kumar

Reputation: 3681

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>

Upvotes: 10

Corey Adler
Corey Adler

Reputation: 16137

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.

Upvotes: 23

Related Questions