使用 Entity Framework 6 和 SQL Server Compact 4

Using Entity Framework 6 with SQL Server Compact 4

我正在尝试使用代码优先方案从代码创建一个 SQL Server Compact 数据库,利用 Entity Framework。一切正常,直到我意识到我已经安装了 SQL Server 2014 Express 并且它正在使用它(而不是我正在寻找的本地 .sdf 文件)。因此,我卸载了 SQL Server 2014 Express。

现在,在我的解决方案中,我正在使用 EntityFramework.SqlServerCompact nuget 扩展。

但是,每当我尝试初始化 DbContext 时,我都会收到错误消息:

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"

我尝试将 app.config 中的连接字符串设置为:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
    </providers>
  </entityFramework>
  <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>
  <connectionStrings>
    <add name="CompactDBContext"
         providerName="System.Data.SqlServerCe.4.0"
         connectionString="MyDatabase.sdf"/>
  </connectionStrings>
</configuration>

或者在代码中设置它,将其传递给 DbContext 构造函数,连接字符串的不同变体等...

这是 DbContext 声明:

 public class CompactDBContext : DbContext
 {
     public DbSet<MyItem> MyItems { get; set; }
 }

这就是我的使用方式:

public void InsertItem()
{
    using (var db = new CompactDBContext())
    {
        MyItem item = new MyItem();

        db.MyItems.Add(item);

        db.SaveChanges();
    }
}

我很确定这是一个重复的问题,或者我遗漏了一些非常微不足道的东西,但我花了几个小时来解决这个问题,但没有任何运气。

谁能指出正确的方向?

更新 1:

通过使用以 SqlCeConnection 作为参数的 DbContext 构造函数:

public CompactDBContext() : 
  base(new SqlCeConnection("Data Source=MyDatabase.sdf;Persist Security Info=False;"),
  contextOwnsConnection: true) { }

我收到这个错误:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.4.0'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

在解决方案和项目中摆弄后,我找到了原因。在我的设置中,我的应用程序是库,我正在从 UnitTest 项目中测试它。出于某种原因,我错误地从 UnitTest 项目中卸载了 app.config,因此它抱怨缺少 EntityFramework 提供程序。将 app.confing 加载回项目修复了问题。

验证您是否在

中安装了 System.Data.SqlServerCE.dll

C:\Program Files\Microsoft SQL Server Compact Edition\v4.0\Private

如果不是,您必须在 SSCERuntime-ENU.msi 之前从这里安装:

https://www.microsoft.com/es-es/download/details.aspx?id=17876