MVC 3 连接字符串到 Sql Server 2008 R2 无法连接
我有 MvcMovie 项目,并且只是添加了一个连接字符串来访问我的 Sql Server 2008 R2 版本中名为 Articles 的类。
我得到了错误。
提供程序未返回 ProviderManifestToken 字符串。内 异常:{“发生与网络相关或特定于实例的错误 同时建立与 SQL Server 的连接。服务器没有 已找到或无法访问。验证实例名称是否正确 并且 SQL Server 配置为允许远程连接。 (提供程序:共享内存提供程序,错误:40 - 无法打开 连接到 SQL Server)”}
我添加到项目中的所有内容是 1. 连接字符串。
<connectionStrings>
<add name="ArticleDB" connectionString="Data Source=Home-PC\SqlServer2008;initial catalog=DairyPump3;integrated security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
名为 Article 的类
公共课文章 { 公共 int ID { 获取;放; } 公共字符串菜单{获取;放; } 公共字符串页面{获取;放; } //公共日期时间发布日期{获取;放; } 公共字符串内容{获取;放; } //公共小数价格 { get;放; } } 公共类 ArticleDBContext :DbContext { 公共 ArticleDBContext() : 基("ArticleDB") { } 公共 DbSet<文章>;文章{获取;放; } }
引用 EF 等的 ArticlesController
我启用了 Sql Server tcp 和管道等,并且允许远程连接。我尝试过先创建数据库,然后删除它 - 但没有区别。 据我所知,我没有使用 Code First - 至少我不想使用 Code First。 该应用程序在 MvcMusic 端运行,但配置文件没有为此的连接字符串 - 我不知道它在哪里存储音乐数据。
我用谷歌搜索过这个问题,很多人都遇到过这个问题,但他们的解决方案似乎没有解决这个问题。
谢谢
I have the MvcMovie project and have simply aadded a connection string to get to my Sql Server 2008 R2 version for a class called Articles.
I get the error.
The provider did not return a ProviderManifestToken string. Inner
Exception: {"A network-related or instance-specific error occurred
while establishing a connection to SQL Server. The server was not
found or was not accessible. Verify that the instance name is correct
and that SQL Server is configured to allow remote connections.
(provider: Shared Memory Provider, error: 40 - Could not open a
connection to SQL Server)"}
All I have added to the project is
1. a connection string.
<connectionStrings>
<add name="ArticleDB" connectionString="Data Source=Home-PC\SqlServer2008;initial catalog=DairyPump3;integrated security=SSPI" providerName="System.Data.SqlClient"/>
</connectionStrings>
A class called Article
public class Article { public int ID { get; set; } public string Menu { get; set; } public string Page { get; set; } //public DateTime ReleaseDate { get; set; } public string Content { get; set; } //public decimal Price { get; set; } } public class ArticleDBContext : DbContext { public ArticleDBContext() : base("ArticleDB") { } public DbSet<Article> Articles { get; set; } }
A ArticlesController referencing EF etc
I have Sql Server tcp and pipes enabled etc, and I allow remote connections. I have tried with creating the db first and then deleting it - but no difference.
I am not using Code First to my knowledge - at least I do not want to use code first.
The app runs for the MvcMusic side but the config file has no connection string for this - I don't know where it stores the music data.
I have googled this and many people have encountered the issue, but their solutions do not seem to address this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的构造函数重载用于指定连接字符串(“Data source=...”)或默认 SQL 实例中的数据库名称(例如“Initial Catalog=ArticleDb”)。
如果您将连接字符串命名为
ArticleDBContext
(或将上下文命名为ArticleDB
),则它应该使用您指定的连接字符串,并且您也不需要构造函数。注意:我使用 EF CF 和迁移对此进行了快速确认。当我使用
: base("foo")
时,我看到 .\SQLEXPRESS (EF 使用的默认实例)上出现了一个名为 foo 的新数据库。我的观察似乎也得到了证实 此处。[编辑:] 另外值得指出的是:当我使用构造函数(使用
: base("foo)"
)时,它覆盖了我在 web.config 中的设置。如果您要部署到需要配置连接字符串的服务器,那么您可能不想这样做。The constructor overload you're using is to specify the connection string ("Data source=...") or the database name at the default SQL instance (e.g. "Initial Catalog=ArticleDb").
If you name your connection string to
ArticleDBContext
(or your context toArticleDB
), it should use the connection string you've specified, and you don't need the constructor either.Note: I did a quick confirmation of this using EF CF with migrations. When I used
: base("foo")
I saw a new database named foo come up on .\SQLEXPRESS (the default instance used by EF). My observations also seem to be confirmed here.[edit:] Also worth pointing out: when I used the constructor (with
: base("foo)"
), it overrode the settings I had in web.config. Probably not something you want to do if you're deploying to a server where you need to configure the connection string.