用于实体框架代码的SQL Server Express连接字符串 [英] SQL Server Express connection string for Entity Framework Code First

查看:1066
本文介绍了用于实体框架代码的SQL Server Express连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Web Developer 2010 Express工作,并使用实体框架的代码优先CTP。我可以使用新的SQL Server CE执行此操作,但是我无法找到与SQL Server Express一起使用的连接字符串。



这一个使用SQL Server CE测试版工作正常(数据库创建并在模型更改中重新创建)。

 < add name =TrempimModel
connectionString =data source = | DataDirectory | TrempimModel.sdf
providerName =System.Data.SqlServerCe.4.0/>

这一个,我从 aspnetdb 连接字符串

 < add name =TrempimModel
connectionString =data source = .\SQLEXPRESS ;集成安全性= SSPI;
AttachDBFilename = | DataDirectory | TrempimModel.mdf;用户实例= true
providerName =System.Data.SqlClient/>

启动时提供以下消息:


无法完成操作。提供的SqlConnection不指定初始目录


那么如何使它与SQL Server Express一起工作?

解决方案

这里的连接字符串的问题是:

 < add name =TrempimModel
connectionString =data source = .\SQLEXPRESS;集成安全性= SSPI;
AttachDBFilename = | DataDirectory | aspnetdb.sdf;
用户实例= true
providerName =System.Data.SqlClient/>

您基本上定义了要连接的服务器 - 但您是说要连接到的文件中的什么数据库。 另外 - SQL Server Express数据库文件的文件扩展名为 .mdf (而不是 .sdf - 这是SQL服务器精简版) - 您还需要考虑到这一点!(根据OP 的评论,是一个打字错误)



您需要定义一个额外的 database = .... (或 Initial Catalog = ..... )在您的连接字符串中:

 < add name =TrempimModel
connectionString =data source = \SQLEXPRESS; Integrated Security = SSPI;
database = YourDatabaseName;
AttachDBFilename = | DataDirectory | aspnetdb.mdf;
用户实例= true
providerName =System.Data。 SqlClient/>

然后它应该工作正常。



有关更多背景和大量样本,请查看 ConnectionStrings 网站。


I am working in Visual Web Developer 2010 Express, and using the Entity Framework code-first CTP. I am able to do this with the new SQL Server CE but I am unable to find a connection string to work with SQL Server Express.

This one, using the SQL Server CE beta, works fine (database is created and recreated on model changes).

<add name="TrempimModel"
     connectionString="data source=|DataDirectory|TrempimModel.sdf"
     providerName="System.Data.SqlServerCe.4.0" />

This one, which I copied from the aspnetdb connections string,

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
     AttachDBFilename=|DataDirectory|TrempimModel.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

Gives the following message on startup:

Unable to complete operation. The supplied SqlConnection does not specify an initial catalog

So how to make it work with SQL Server Express?

解决方案

The problem with your connection string here is:

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.sdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

You're basically defining what "server" you're connecting to - but you're not saying what database inside the file to connect to. Also - the file extension for SQL Server Express database files is .mdf (not .sdf - that's SQL Server Compact Edition) - you need to take that into account, too! (was a typo, according to comment by OP).

You need to define an extra database=.... (or Initial Catalog=.....) in your connection string:

<add name="TrempimModel"
     connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
                       database=YourDatabaseName;
                       AttachDBFilename=|DataDirectory|aspnetdb.mdf;
                       User Instance=true"
     providerName="System.Data.SqlClient" />

Then it should work just fine.

For more background and tons of samples, check out the ConnectionStrings web site.

这篇关于用于实体框架代码的SQL Server Express连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆