加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – MVC 4中的SQL Server Express数据库文件自动创建

发布时间:2020-12-16 07:17:25 所属栏目:asp.Net 来源:网络整理
导读:我刚刚在ASP.Net MVC 4中部署了一个新的应用程序.我使用的是SQL Server 2008 R2(不是SQL Express). 它在前10分钟工作得很好,然后我对代码进行了一些改动并重新部署了它. 现在,每当我尝试访问使用SimpleMembership的页面时,我都会收到此错误: A network-rela
我刚刚在ASP.Net MVC 4中部署了一个新的应用程序.我使用的是SQL Server 2008 R2(不是SQL Express).

它在前10分钟工作得很好,然后我对代码进行了一些改动并重新部署了它.

现在,每当我尝试访问使用SimpleMembership的页面时,我都会收到此错误:

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: SQL Network Interfaces,error: 26 – Error Locating Server/Instance Specified)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

SQLExpress database file auto-creation error:
The connection string specifies a local Sql Server Express instance using a database location within the application’s App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

但是,我不使用SQL Server Express.在我的web.config中,我设置了所有连接字符串,如下所示:

<add name="ApplicationServices" 
     connectionString="Server=myServer;Database=myDB;User Id=myUserID;Password=myPWD;" 
     providerName="System.Data.SqlClient" />

<add name="ApplicationServices" 
     connectionString="Server=myServer;Database=myDB;User Id=myUserID;Password=myPWD;" 
     providerName="System.Data.SqlClient" />

为什么它一直在尝试创建SQL Server EXPRESS数据库?

解决方法

当我遇到这个错误时,结果很简单.我终于发现在我的_Layout.cshtml中我引用了User.IsInRole(“role”)但是在Request.isAuthenticated之后.我的代码看起来像@if(Request.isAuthenticated&& User.IsInRole(“role”)).所以基本上如果我没有在主页上签名就会渲染,因为它不会遍历到IsInRole()调用(这需要初始化简单的成员资格,否则我会得到你在这里提到的错误.所以基本上我需要确保使用扩展_Layout文件的视图并且可能具有未经身份验证的用户的每个控制器都需要具有[InitializeSimpleMembership].或者初始化简单成员资格的各种方法之一.

所以像

namespace ProjectName.Controllers
{
    [InitializeSimpleMembership]
    public class HomeController : Controller {
...

此外,我经常按Ctrl F5启动应用程序,然后使用“重建解决方案”.当我刷新一个不是根主页的页面时,我会收到错误,直到我调用root主页.那时,一切都会重新开始.

希望这会对你或某人有所帮助.我撞了我的头至少一两个小时.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读