使用 IIS 與 LocalDB 遇到的例外錯誤

  • 2781
  • 0

使用的專案範本是 MVC,網站執行起來,就能做登入帳號密碼驅動 Entity Framework Code First 建立 Web.config 所設定的 LocalDB 資料庫檔案,


IIS 需要連接 LocalDB 資料庫時,可能會發生的例外錯誤紀錄,

 

  • applicationHost.config 裡的 loadUserProfile 或 setProfileEnvironment 沒設定 true 的錯誤訊息:
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 get a local application data path. Most probably a user profile is not loaded. If LocalDB is executed under IIS, make sure that profile loading is enabled for the current user. )

解決方法: "C:\Windows\System32\inetsrv\config\applicationHost.config" applicationPoolDefaults 設定參考,

        <applicationPools>
            <add name="DefaultAppPool" autoStart="true" managedRuntimeVersion="v4.0" />
            <add name=".NET v4.5 Classic" managedRuntimeVersion="v4.0" managedPipelineMode="Classic" />
            <add name=".NET v4.5" managedRuntimeVersion="v4.0" />
            <applicationPoolDefaults managedRuntimeVersion="v4.0">
                <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />
            </applicationPoolDefaults>
        </applicationPools>

 

  • LocalDB 執行個體不存在的例外錯誤訊息:
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. The specified LocalDB instance does not exist. )

 解決方法:

  1. 先用 Command Line 列出 LocalDB 有哪些執行個體
  2. 修改 Web.config 的連線字串改連存在的 LocalDB 執行個體名稱,例如 MSSQLLocalDB
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\WebApplication1.mdf;Initial Catalog=WebApplication1;Integrated Security=True"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

     

  •  停止應用程式集區,刪除 mdf 資料庫檔案後,在啟動應用程式集區,網站要連接 LocalDB時的例外錯誤:
Cannot attach the file 'C:\WebApplication1\WebApplication1\App_Data\WebApplication1.mdf' as database 'WebApplication1'.

 解決方法:更換連線字串的 Initial Catalog 名稱。

 

  • 不能自動建立執行個體
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.)

忘了怎麼出現的,後來都沒試出來…

 

 

參考文章

https://support.microsoft.com/zh-tw/kb/2547655 

http://vmiv.blogspot.tw/2012/03/q-localdb-iis.html