2016-03-07 67 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中的数据库。然后,我右键单击数据库,然后选择“属性”,然后将整个连接字符串复制到我的配置中 - 这对我来说一直适用。没有看你的代码,我不能更具体,对不起 –