2016-03-07 69 views
0

我有一個正常的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=&quot;data source=.\SQLEXPRESS;initial catalog=ProdStore;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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

+0

您是否找到了解決方案呢?我面臨同樣的問題。 – maaizle

回答

0

您連接到的SQL Server實例需要允許同一預訂中的其他Azure服務。它不會允許來自不同訂閱的連接,只需轉到SQL Server的配置,並確保在IP配置下驗證「Windows Azure服務」設置爲YES。如果這仍然失敗,那麼您的連接字符串中可能有錯誤。

用戶名通常是user @ servername,不要忘記密碼:)

+0

嗨佩德羅,感謝您的答案,但Windows Azure服務已啓用。我已經嘗試過所有合適的連接字符串,但沒有運氣,現在有點遺憾。 – Programmer

+0

每當我對Azure SQL進行操作時,通常會打開Visual Studio中的「SQL對象資源管理器」選項卡(使用CTRL + Q查找它),然後連接到Azure中的數據庫。然後,我右鍵單擊數據庫,然後選擇「屬性」,然後將整個連接字符串複製到我的配置中 - 這對我來說一直適用。沒有看你的代碼,我不能更具體,對不起 –