我有一個正常的Web API項目在蔚藍使用下面的連接字符串運行連接的Azure數據庫:連接字符串更新了的WebAPI項目
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-CatalogAPI-20160227031830.mdf;Initial Catalog=aspnet-CatalogAPI-20160227031830;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ProdStoreEntities" connectionString="metadata=res://*/DbCache.csdl|res://*/DbCache.ssdl|res://*/DbCache.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=ProdStore;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
當我發佈這個項目,在設置選項卡我使用我的Azure數據庫的連接字符串,看起來像這樣。
Server=tcp:{name}.database.windows.net,1433;Database=ProductDatabase;User ID={user};Password={your_password_here};Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
這一切工作正常。
然後,我通過在Visual Studio中右鍵單擊我的web api項目並選擇Add - > New Azure Web Job Project創建了一個Web作業。
在我的web工作項目中,我對我的web api項目做了一個解決方案參考,並編寫了一些在我的web api項目中使用存儲庫的代碼。
我將我的web api項目中完全相同的連接字符串添加到我的web作業項目中,並且本地一切正常。當我將所有內容發佈到Azure時,我都會遇到此錯誤。
Unhandled Exception: System.Data.SqlClient.SqlException: 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)
我的問題是,我的Azure Web Job項目中需要什麼類型的連接字符串?我假設它需要在某處引用Azure數據庫,但是當我嘗試獲取此錯誤時。
System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
您是否找到了解決方案呢?我面臨同樣的問題。 – maaizle