Entity Framework Code First 的 SQL Server Express 连接字符串 [英] SQL Server Express connection string for Entity Framework Code First

查看:35
本文介绍了Entity Framework Code First 的 SQL Server Express 连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Web Developer 2010 Express 中工作,并使用实体框架代码优先 CTP.我可以使用新的 SQL Server CE 执行此操作,但无法找到与 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.

这个使用 SQL Server CE 测试版,运行良好(在模型更改时创建和重新创建数据库).

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" />

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

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:

无法完成操作.提供的 SqlConnection 没有指定初始目录

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

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

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" />

您基本上是在定义要连接到的服务器" - 但您没有说明要连接到文件中的哪个数据库.另外 - SQL Server Express 数据库文件的文件扩展名是 .mdf(不是 .sdf - 那是 SQL Server Compact Edition) - 你需要考虑到这一点,也是!(打错字,根据 OP 的评论).

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).

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

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.

有关更多背景和大量示例,请查看 ConnectionStrings 网站.

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

这篇关于Entity Framework Code First 的 SQL Server Express 连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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