[英]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.