将 SQL Server Compact 4.0.0.1 与 Entity Framework 4.3 结合使用

发布于 2025-01-06 14:53:31 字数 2525 浏览 6 评论 0原文

错误:

无法加载 System.Data.SqlServerCe.Entity.dll。重新安装 SQL Server Compact。

内部异常:

{"无法加载文件或程序集 'System.Data.SqlServerCe.Entity, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91' 或其其中之一找到的程序集的清单定义与程序集引用不匹配(来自 HRESULT 的异常: 0x80131040)":"System.Data.SqlServerCe.Entity, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91"}

重现步骤:

  1. 创建新的 MVC3 应用程序

  2. 添加Nuget包EntityFramework.SqlServerCompact

  3. 创建模型

    公共类TaskItem
    {
        [钥匙]
        公共 int Id { 得到;放; }
        公共字符串描述{获取;放; }
    }
    
  4. 创建 DbContext

    公共类 TestContext :DbContext
    {
        公共 DbSet;任务项 { 获取;放; }
    }
    
  5. Home 控制器

    公共 ActionResult Index()
    {
        var db = new TestContext();
        // 下面的 Add() 上的断点
        db.TaskItems.Add(new TaskItem { Description = "完成任务。"});
        返回视图();
    }
    
  6. Web.config 连接字符串和运行时和System.Data

    <前><代码><运行时>; < assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <依赖程序集> < assemblyIdentity name =“System.Web.Mvc” publicKeyToken =“ 31bf3856ad364e35” /> ><依赖程序集> < assemblyIdentity name =“System.Data.SqlServerCe”publicKeyToken =“89845dcd8080cc91”文化=“中性”/> ><系统数据><删除 invariant="System.Data.SqlServerCe.4.0" />

问题:

Web.config 都是默认的...这里没有什么奇怪的...所有 DLL 都复制到本地并在 bin 中...我缺少什么?这一定是非常简单的事情。


更新:

从 web.config 中删除依赖程序集部分解决了问题,但我仍然需要解释原因才能结束答案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2025-01-13 14:53:31

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

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>
独自唱情﹋歌 2025-01-13 14:53:31

检查此代码是否在您的项目中创建

EntityFramework.SqlServerCompact.cs under the App_Start folder

using System.Data.Entity;
using System.Data.Entity.Infrastructure;

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyStory.Tests.App_Start.EntityFramework_SqlServerCompact), "Start")]

namespace MyStory.Tests.App_Start {
    public static class EntityFramework_SqlServerCompact {
        public static void Start() {
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
        }
    }
}

Check this code is created or not on your project

EntityFramework.SqlServerCompact.cs under the App_Start folder

using System.Data.Entity;
using System.Data.Entity.Infrastructure;

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyStory.Tests.App_Start.EntityFramework_SqlServerCompact), "Start")]

namespace MyStory.Tests.App_Start {
    public static class EntityFramework_SqlServerCompact {
        public static void Start() {
            Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文