使用Entity Framework 6和SQL Server Compact 4

4
我正在尝试使用代码优先的方案,利用Entity Framework创建SQL Server Compact数据库。一切都很顺利,直到我意识到我安装了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) { }

我遇到了这个错误:

“System.InvalidOperationException”类型的异常在EntityFramework.dll中发生,但未在用户代码中处理

额外信息:找不到与不变名称为“System.Data.SqlServerCe.4.0”的ADO.NET提供程序对应的Entity Framework提供程序。请确保该提供程序已在应用程序配置文件的“entityFramework”部分中注册。有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkId=260882


你是如何初始化 dbcontext 的? - Erik Philips
1
你能分享一下app.config中与Entity Framework相关的部分吗?你仍然连接到SQL Server。 - ErikEJ
@ErikEJ:添加了代码片段和app.config。 - user969153
2个回答

1
在调试解决方案和项目时,我发现了问题所在。在我的设置中,我的应用程序是库,而我是从 UnitTest 项目进行测试的。由于某种原因,我错误地卸载了 UnitTest 项目中的 app.config 文件,因此它会抱怨缺少 EntityFramework 提供程序。将 app.confing 文件重新加载到项目中解决了问题。

类库项目将使用主项目的 app.config。您如何从单元测试项目中卸载 app config? - user6490459

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接