Azure的应用程序到Azure数据库“连接失败”与实体框架
我有一个C#.NET 的Web API部署到Azure的应用服务,我也有一个Azure的SQL数据库。Azure的应用程序到Azure数据库“连接失败”与实体框架
在API中,我使用实体框架插入数据库,但我不断收到错误消息:“底层提供程序在打开时失败”。
(当在本地运行API(在调试模式下)连接到本地数据库时,它工作正常)。 这可能是Azure数据库的权限/防火墙配置问题,还是其他?
我在“Azure集服务器防火墙”中添加了当前的IP地址,是否需要将Azure Web API的IP地址添加到数据库防火墙设置?
这是我的API:
public class ProfileController : ApiController
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[WebMethod]
[HttpPost]
public HttpResponseMessage PostProfile([FromBody] Profile details)
{
var context = new XXXDBEntities();
var query = from c in context.Users
where c.Email.Equals(details.email, StringComparison.CurrentCultureIgnoreCase)
select c;
var emailFound = query.Count();
if (emailFound != 0)
{
return Request.CreateResponse(HttpStatusCode.OK, "There is already an account associated with this email address");
}
else
{
Guid token = Guid.NewGuid();
Users newRow = new Users();
newRow.Token = token;
newRow.FirstName = details.firstName;
newRow.LastName = details.lastName;
newRow.Email = details.email;
newRow.Password = details.password;
context.Users.Add(newRow);
context.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, token);
}
}
这是我的连接字符串: 这是在.net实体框架的连接字符串的默认格式,我只加了用户名,密码和更改的数据源和目录字段。它是否正确?
<add name="XXXDBEntities" connectionString="metadata=res://*/XXXDB.csdl|res://*/XXXDB.ssdl|res://*/XXXDB.msl;provider=System.Data.SqlClient;provider connection string="data source=tcp:XXX.database.windows.net,1433;initial catalog=XXXDB;integrated security=True;User ID=XXXXX;Password=XXXXX;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
我检查了您的连接字符串,它似乎是正确的。所以我认为你的问题可能是由SQL Azure的模型引起的,但是你的项目不会更新它。下面是我在我身边再现了同样的问题:
我建议你更新你的模型。
是否需要将Azure Web API的IP地址添加到数据库防火墙设置?
我们可以设置允许在SQL Azure中的防火墙设置访问Azure的服务ON。所以我们不需要添加Azure Web API的地址。
是的,我已经设置它为真,谢谢。它正在改变连接字符串,这解决了我的问题。 – Tess
你需要从连接字符串中删除Integrated Security=True
因为你指定一个用户名和密码。
您能够在本地运行,但仍能成功连接到您的Azure SQL实例吗? –
当您指定用户名和密码时,为什么在连接字符串中有'integrated security = True'? – juunas
谢谢!我删除了“集成安全性=真”,并添加了“坚持安全信息=真”。它现在连接成功:) – Tess