Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SQL Server Compact 4.0.0.1 with Entity Framework 4.3

Error:

Could not load System.Data.SqlServerCe.Entity.dll. Reinstall SQL Server Compact.

Inner Exception:

{"Could not load file or assembly 'System.Data.SqlServerCe.Entity, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Data.SqlServerCe.Entity, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"}

Steps to Reproduce:

  1. Create new MVC3 Application

  2. Add Nuget Package EntityFramework.SqlServerCompact

    <package id="EntityFramework.SqlServerCompact" version="4.1.8482.2" />

  3. Create Model

    public class TaskItem
    {
        [Key]
        public int Id { get; set; }
        public string Description { get; set; }
    }
    
  4. Create DbContext

    public class TestContext : DbContext
    {
        public DbSet<TaskItem> TaskItems { get; set; }
    }
    
  5. Home Controller

    public ActionResult Index()
    {
        var db = new TestContext();
        // breakpoint on Add() below
        db.TaskItems.Add(new TaskItem { Description = "Get shit done."});
        return View();
    }
    
  6. Web.config Connection String and Runtime and System.Data

    <add name="ApplicationServices" connectionString="Data Source=|DataDirectory|Test.sdf" providerName="System.Data.SqlServerCe.4.0" />

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
            <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <system.data>
        <DbProviderFactories>
          <remove invariant="System.Data.SqlServerCe.4.0" />
          <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
      </system.data>
    

Question:

Web.config is all default... nothing funky here... all the DLLs are copy local and in bin... what am I missing? It's got to be something very simple.


Update:

Removing the Dependent Assembly section from web.config solved the problem, but I still need an explanation why in order to close the answer.

like image 399
one.beat.consumer Avatar asked Feb 17 '12 02:02

one.beat.consumer


1 Answers

Remove the dependent assembly binding from your web.config

  <dependentAssembly>
    <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.0.1" newVersion="4.0.0.1" />
  </dependentAssembly>
like image 50
Kori Avatar answered Nov 07 '22 01:11

Kori