c# - SqlServerCe 的实体连接字符串

标签 c# entity-framework connection-string

如何为 SqlServerCe 数据库创建一个 EntityConnection 字符串?

我尝试按照 How to: Build an EntityConnection Connection String 上的 MSDN 示例进行操作

const string PROVIDER = "System.Data.SqlClient";

static string BuildEntityConnString(string dbFileName, string password) {
  // dbFileName is filename without the extension
  SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
  sqlBuilder.DataSource = PROVIDER;
  sqlBuilder.InitialCatalog = dbFileName;
  sqlBuilder.IntegratedSecurity = true;
  sqlBuilder.Password = password;
  string providerString = sqlBuilder.ToString();
  EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
  entityBuilder.Provider = PROVIDER;
  entityBuilder.ProviderConnectionString = providerString 
  entityBuilder.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", dbFileName);
  string connString = entityBuilder.ToString();
  using (EntityConnection con = new EntityConnection(connString)) {
    con.Open();
    Console.WriteLine("{0} Entity String created.", dbFileName);
    con.Close();
  }
  return connString;
}

然而,却抛出一个错误:

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: Named Pipes Provider, error: 40 - Could not open 
a connection to SQL Server)

With some searching on here, I was able to find info stating that the MSDN Example returns an SqlClient connection.

However, I was not able to find out how to return an Sql CE Connection.

I tried supplying the EntityConnection the SQL CE Connection String that allows me to open the .SWF database and read it conventionally:

const string PROVIDER = "Microsoft.SqlServerCe.Client.3.5";

static string BuildEntityConnString(string dbFileName, string password) {
  EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
  entityBuilder.Provider = PROVIDER;
  entityBuilder.ProviderConnectionString = myCeConnectionString;
  entityBuilder.Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", dbFileName);
  string connString = entityBuilder.ToString();
  using (EntityConnection con = new EntityConnection(connString)) {
    con.Open();
    Console.WriteLine("{0} Entity String created.", dbFileName);
    con.Close();
  }
  return connString;
}

此版本抛出不同的错误:

Unable to find the requested .Net Framework Data Provider.
It may not be installed.

我做错了什么?

我发现这个相关 SQL Server Fix ,但修复需要是我可以在我的代码中设置的内容,这样我的项目就可以在没有网络管理员的情况下在其他位置的客户计算机上运行。

最佳答案

您需要在您的 app.config 中注册 CE 提供程序。另外,据我所知,“Microsoft.SqlServerCe.Client.3.5”是提供程序的预发布名称; RTM 是“System.Data.SqlServerCe.3.5”。

尝试更改 PROVIDER 以匹配第二个字符串,并将其添加到 app.config 中:

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SqlServerCe.3.5" />
      <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"
      />
    </DbProviderFactories>
  </system.data>

关于c# - SqlServerCe 的实体连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6113724/

相关文章:

c# - 从枚举自动生成 DataGrid 列

c# - 将打印页面图形转换为位图 C#

entity-framework - Entity Framework 代码优先和连接字符串问题

c# - 使用代码优先使用实体​​框架检查表是否为空

entity-framework - Entity Framework 代码优先只读集合

mysql - 如何在 Visual Fox Pro 中保持 MySQL 连接处于事件状态

比较 C 中两个设置位置(整数值)之间两个字符串的两个子字符串

c# - 在 C# 中构建字符串数组并检查空值?

c# - 在对象成为孤立对象之前,我是否需要从对象中删除事件订阅?

c# - 如何找到 Entity Framework 对每个传入请求进行的数据库调用次数?