2015-08-25 81 views
0

我正在學習官方網站的ASP.NET MVC。SQL Server連接問題(ASP.Net MVC)

當我到達this tutorial page,創建Movies控制器後,當我導航到url:localhost:// xxxx/movies/index我收到一個異常。

異常消息是:

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: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details

我的系統細節: -

  • 的Windows 10 PRO(64位)
  • 社區2015年
  • 的SQL Server 2014的Visual Studio (64位)
0123在web.config文件

連接字符串是這樣的

<connectionStrings> 
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=&quot;|DataDirectory|\aspnet-world movies-20150825083408.mdf&quot;;Initial Catalog=&quot;aspnet-world movies-20150825083408&quot;;Integrated Security=True" providerName="System.Data.SqlClient" /> 
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> 

唯一的例外是在這個動作方法在行#20

public ActionResult Index() 
Line 19:   { 
Line 20:    return View(db.Movies.ToList()); 
Line 21:   } 
+0

請出示與從web配置您的SQL連接字符串代碼,顯然移除所有憑證信息。 – zulq

+1

您完成了第5步嗎? –

回答

1

它認爲這個問題是連接字符串。

當教程它使用SQL Server 2012的LocalDB然後連接字符串中"Data Source=(LocalDB)\v11.0;"

但在VS 2015年的SQL Server 2014則改爲"(localdb)\ProjectsV12;"

更新發布:微軟再次改變了的LocalDB名VS 2015年 使用"(LocalDB)\MSSQLLocalDB;"

所以儘量連接字符串更改爲

"(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf" 

並嘗試運行。好運

你可以找到工作的代碼的理解:GettingStartedWithASPNETMVC5

+0

嘿Jophy工作感謝您的回覆 我試圖改變連接字符串,因爲你建議,但以下異常已被捕獲。 不支持關鍵字:'(localdb)\ projectsv12; attachdbfilename'。 –

+0

問題在於此操作方法: public ActionResult Index() { return View(db.Movies.ToList()); } –

+0

抱歉,Microsoft再次更改了localDB數據庫名稱。請使用「數據源=(LocalDB)\ MSSQLLocalDB」 –