简体   繁体   English

实体框架代码的SQL Server Compact 4.0连接字符串首先?

[英]SQL Server Compact 4.0 connection string for Entity Framework code first?

I am creating an application for Windows XP so I am stuck with .Net framework 4.0. 我正在为Windows XP创建一个应用程序,所以我坚持使用.Net framework 4.0。 I tried to use SQL Server Compact and EF code first but it make an error when update-database . 我首先尝试使用SQL Server Compact和EF代码,但在update-database时出错。

I wish to put the database in my code directory to deploy in customer machine. 我希望将数据库放在我的代码目录中以部署在客户机器中。

This is my connection string: 这是我的连接字符串:

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

And the error: 而错误:

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. 提供未打开的连接。

In one of my projects I used SQL Server Compact 4.0 and my connection string was simple enough: 在我的一个项目中,我使用了SQL Server Compact 4.0,我的连接字符串很简单:

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

I think that you should check your providerName . 我认为你应该检查你的providerName I am not sure if System.Data.SqlClient is the right one. 我不确定System.Data.SqlClient是否正确。

Have you installed EntityFramework.SqlServerCompact NuGet package ? 你安装了EntityFramework.SqlServerCompact NuGet包吗? Once it is installed System.Data.SqlServerCe.4.0 provider name is added and it should be used in the connection string. 一旦安装,就会添加System.Data.SqlServerCe.4.0提供程序名称 ,并且应该在连接字符串中使用它。

Check if SqlServerCompact provider is added to your web.config . 检查是否已将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>

To prevent this you need to change your connection string to have: 要防止这种情况,您需要更改连接字符串以使其具有:

 Trusted_Connection=False;Persist Security Info=True

based on below link 基于以下链接

http://jayhollingum.blogspot.in/2011/03/ef-codefirst-databasesetinitializser.html http://jayhollingum.blogspot.in/2011/03/ef-codefirst-databasesetinitializser.html

After many search, I decide to move to a near approach of SQL compact, it's LocalDB. 经过多次搜索,我决定转向SQL compact的近距离接近,它是LocalDB。 I change it and this work: 我改变它,这项工作:

<add name="QuanLyKhoContext" connectionString="Data Source=(LocalDB)\v11.0;
AttachDbFilename=|DataDirectory|MyDB.mdf;Integrated Security=True;Connect Timeout=60" providerName="System.Data.SqlClient" />

I think "System.Data.SqlClient" is a valid Provider because I am using Sql Express LocalDB :) It's not my first purpose, but it work anyway. 我认为“System.Data.SqlClient”是一个有效的Provider,因为我使用的是Sql Express LocalDB :)这不是我的第一个目的,但无论如何它都可以工作。 Thanks all 谢谢大家

I just don't know when deploy in window XP, do I need to install Sql Express or Sql Compact stuffs manually? 我只是不知道什么时候在Windows XP中部署,我需要手动安装Sql Express或Sql Compact的东西吗?

According to MSDN documentation you can use the following: 根据MSDN文档,您可以使用以下内容:

<add name="ConnectionStringName"
    providerName="System.Data.SqlServerCe.4.0"
    connectionString="Data Source=|DataDirectory|\DatabaseFileName.sdf" />

PS: How to install SQL Server Compact PS: 如何安装SQL Server Compact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有实体框架的条件字符串比较的SQL Server Compact 4.0 - SQL Server Compact 4.0 with Entity Framework Conditional String Comparison 如何告诉Entity Framework首先在代码中使用SQL Server Compact? - How do I tell Entity Framework to use SQL Server Compact in code first? 实体框架代码中的种子主键在SQL Server Compact Edition中首先为0 - Seed primary key in Entity Framework code first to 0 in SQL Server Compact Edition 如果我们使用具有代码优先方法的Entity框架,是否可以在给定路径上创建数据库(Sql Server compact)? - Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach? 实体框架6.1.3和SQL Server CE 4.0-代码优先设置密码以保护sdf文件 - Entity Framework 6.1.3 and SQL Server CE 4.0 - Code First Setup a password to protect sdf file Code First Entity Framework - 更改连接字符串 - Code First Entity Framework - change connection string 实体框架和SQL Server数据库连接字符串 - Entity Framework and SQL Server database connection string 外部SQL服务器上的实体框架代码优先 - Entity Framework Code First on external SQL server 具有实体框架的SQL Server Compact Edition - SQL Server Compact Edition with Entity Framework 将Entity Framework 6与SQL Server Compact 4一起使用 - Using Entity Framework 6 with SQL Server Compact 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM