实体框架 Code First 的 SQL Server Express 连接字符串

发布于 2024-10-01 04:11:40 字数 790 浏览 2 评论 0原文

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

这个使用 SQL Server CE beta 运行良好(数据库是在模型更改时创建和重新创建的)。

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

这个是我从 aspnetdb 连接字符串复制的,

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

在启动时给出以下消息:

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尬尬 2024-10-08 04:11:40

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

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

您基本上定义了要连接到的“服务器” - 但您没有说明要连接到文件中的哪个数据库。 此外 - SQL Server Express 数据库文件的文件扩展名是 .mdf (不是 .sdf - 这是 SQL Server Compact Edition) - 您需要考虑到这一点,也是!(根据OP的评论,是一个拼写错误)。

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

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

然后它应该可以正常工作。

有关更多背景信息和大量示例,请访问 ConnectionStrings 网站。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文