c# - Entity Framework 代码的 SQL Server Compact 4.0 连接字符串优先?

标签 c# entity-framework ef-code-first sql-server-ce connection-string

我正在为 Windows XP 创建一个应用程序,所以我只能使用 .Net framework 4.0。我尝试先使用 SQL Server Compact 和 EF 代码,但在 update-database 时出错。

我希望将数据库放在我的代码目录中以部署在客户机器上。

这是我的连接字符串:

<add name="QuanLyKhoContext" 
     connectionString="Data Source=MyData.sdf;Persist Security Info=False;AttachDBFileName=MyData.sdf" 
     providerName="System.Data.SqlClient" />

错误:

This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection.

最佳答案

在我的一个项目中,我使用了 SQL Server Compact 4.0,我的连接字符串非常简单:

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

我认为您应该检查您的providerName。我不确定 System.Data.SqlClient 是否正确。

你有没有安装 EntityFramework.SqlServerCompact NuGet 包?安装后 System.Data.SqlServerCe.4.0 提供程序名称 被添加,它应该在连接字符串中使用。

检查 SqlServerCompact 提供程序是否已添加到您的 web.config

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="System.Data.SqlServerCe.4.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <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>

关于c# - Entity Framework 代码的 SQL Server Compact 4.0 连接字符串优先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441242/

相关文章:

c# - 如何在 C# 中透明地跟踪字符流中的行号?

c# - 获取 Windows Phone 8 中的电话号码

c# - EF 核心 : How to add the relationship to shadow property?

.net - Entity Framework 中的动态 LINQ 比较日期

linq - linq 为存储过程的两次不同执行返回相同的数据?

c# - 创建家谱结构

javascript - 从混淆的字符串中识别正确的单词(随机位置的意外空格)

entity-framework - EF Code First - 创建数据库 - 用户登录失败

sql-server - 使用 Entity Framework Database.Create() 时指定 SQL Server 文件位置

entity-framework - Entity Framework 4.1Code 首先连接到Sql Server 2005