2013-04-08 65 views
5

我剛剛在ASP.Net MVC 4中部署了一個新應用程序。我使用SQL Server 2008 R2( NOT SQL Express)。MVC 4中的SQL Server Express數據庫文件自動創建錯誤 - 但我不想使用SQL Server 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數據庫?

+0

連接字符串名稱需要與數據庫上下文相同。請參閱http://stackoverflow.com/questions/5346926/code-first-specify-database-name – AaronLS 2013-04-08 22:21:41

+1

對不起,我可能誤解了你的問題。我假設你先使用EF代碼。 – AaronLS 2013-04-08 22:25:21

+1

你解決了這個問題嗎?我面對完全相同的錯誤.. – Chatumbabub 2013-05-22 18:08:10

回答

4

你還有這個問題嗎?另外,您爲什麼要將ApplicationServices列出兩次?

我碰到了同樣的事情。我錯過了web.config中的成員和角色配置。

<membership defaultProvider="AspNetSqlMembershipProvider"> 
    <providers> 
    <clear /> 
    <add name="AspNetSqlMembershipProvider" 
     type="System.Web.Security.SqlMembershipProvider" 
     connectionStringName="ApplicationServices" 
     enablePasswordRetrieval="false" 
     enablePasswordReset="true" 
     requiresQuestionAndAnswer="false" 
     requiresUniqueEmail="false" 
     maxInvalidPasswordAttempts="5" 
     minRequiredPasswordLength="6" 
     minRequiredNonalphanumericCharacters="0" 
     passwordAttemptWindow="10" 
     applicationName="Test" /> 
    </providers> 
</membership> 
<roleManager enabled="true" defaultProvider="SqlRoleManager"> 
    <providers> 
    <add name="SqlRoleManager" 
     type="System.Web.Security.SqlRoleProvider" 
     connectionStringName="ApplicationServices" 
     applicationName="Test" /> 
    </providers> 
</roleManager> 
4

當我遇到這個錯誤時,結果很簡單。我終於明白,在我的_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來啓動應用程序,然後使用「重建解決方案」。當我刷新一個不是根的頁面時主頁,我會得到錯誤,直到我將調用根主頁。那時,一切都會重新開始。

希望這會幫助你或某人。我至少在一個小時或兩個鐘頭裏敲了敲頭。

相關問題