wpf - SQL Server CE 上的 Entity Framework 无需安装驱动程序

标签 wpf entity-framework-4 sql-server-ce

我正在使用 Entity Framework 4 和 SQL Server CE 数据库 3.5 开发 WPF 应用程序,并且它工作正常。

但是,我希望此应用程序在安装了 .Net Framework 4 但没有 SQL Server Compact 3.5 驱动程序的计算机上运行。并且没有安装程序。可能吗?

我尝试过以下方法:

  1. 在 app.config 中创建一个部分

    <configuration>
       <connectionStrings>
          <add name="DbEntities" 
               connectionString="metadata=res://*/Model.Model1.csdl|res://*/Model.Model1.ssdl|res://*/Model.Model1.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=|DataDirectory|\Data\Database1.sdf&quot;" 
               providerName="System.Data.EntityClient"/>
       </connectionStrings>
       <system.data>
          <DbProviderFactories>
              <add name="SQL Server Compact Edition Data Provider" 
                   invariant="System.Data.SqlServerCe" 
                   description=".NET Framework Data Provider for Microsoft SQL Server Compact Edition" 
                   type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
          </DbProviderFactories>
       </system.data>
    </configuration>
    
  2. 添加对 System.Data.SqlServerCeSystem.Data.SqlServerCe.Entity 的引用并允许本地复制

  3. 构建应用程序并将其复制到未安装 SQL Server CE 驱动程序的计算机上。当涉及到创建数据上下文时,我不断收到此错误:

System.ArgumentException: The specified store provider cannot be found in the configuration, or is not valid. --->
System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.

我在浪费时间吗?或者我应该切换到 SQLite?

谢谢!

编辑:

感谢 josemiguel.torres 的回答,我查看 this post详细说明如何实现这一目标。但是,我仍然遇到汇编错误。

"System.IO.FileLoadException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.0.0, 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)"

所以我看了一下this other post解释如何解决此问题。 添加一些程序集绑定(bind)后:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
      <bindingRedirect oldVersion="3.5.1.0-3.5.1.50" newVersion="3.5.1.50"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>

它仍然不起作用...:| 错误消息类似:

System.IO.FileLoadException: Could not load file or assembly 'System.Data.SqlServerCe, Version=3.5.0.0, 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,版本=3.5.0.0,文化=中性,PublicKeyToken=89845dcd8080cc91”

我没有解决方案。有人吗?

编辑2:

1.我已从我的计算机上卸载了所有 Microsoft“SQL Server Compact Edition”版本。

2.我检查了 machine.config 版本:DbProviderFactories 部分中没有条目。

3.我从there下载了3.5 SP2驱动程序并安装了 x86 软件包。

4.我检查 machine.config (v4 x86),并在 DbProviderFactories 节点下创建了以下条目:

<add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

为什么是 3.5.0.0 而不是 3.5.1.50???

以防万一:我的开发机器是 Win7 x64,目标机器是 WinXP x86。

最佳答案

安装3.5 SP2,并修改app.config如下:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ChinookEntities" connectionString="metadata=res://*/Chinook.csdl|res://*/Chinook.ssdl|res://*/Chinook.msl;provider=System.Data.SqlServerCe.3.5;provider connection string=&quot;Data Source=C:\Users\erik.COMMENTOR\Documents\Visual Studio 2010\Projects\SqlCeTest\Chinook.sdf&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5" />
      <add name="Microsoft SQL Server Compact Data Provider 3.5" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.1.50, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
    </DbProviderFactories>
  </system.data>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly xmlns="">
        <assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral" />
        <bindingRedirect oldVersion="3.5.1.0-3.5.1.50" newVersion="3.5.1.50" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

确保使用 C:\Program Files (x86)\Microsoft SQL Server Compact Edition\v3.5\Private 文件夹中的 System.Data.SqlServerCe.dll 和 System.Data.SqlServerCe.Entity.dll

关于wpf - SQL Server CE 上的 Entity Framework 无需安装驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10300373/

相关文章:

c# - WPF DataGrid 在编辑时预填充单元格默认值

WPF DataGrid 与 RowDetailsTemplate 中的 DataGrid

asp.net-mvc-3 - 如何通过依赖注入(inject)使用 Entity Framework 上下文?

sql-server - 如何处理删除可能是 FK 的数据库记录?

c# - 如何从 C# 连接到 SQL Server Ce 数据库?

WPF - 将图像水平绑定(bind)到 ListView

c# - DrawingImage 样式绑定(bind)错误

entity-framework-4 - Entity Framework NullReferenceException 调用 ToList?

C# 和 SQL 命令

c# - SQL Server CE 数据库连接问题