C# Azure应用程序到Azure数据库“;“连接失败”;具有实体框架
我有一个C#NetWeb API部署到Azure应用程序服务,我还有一个Azure SQL数据库 在API中,我使用实体框架插入到数据库中,但我不断收到错误消息:“基础提供程序在打开时失败” (在本地运行API(在调试模式下)并连接到本地数据库时,它工作正常)。 这可能是Azure数据库的权限/防火墙配置问题,还是其他问题 我已将当前IP地址添加到“Azure Set Server Firewall”中,是否需要将Azure Web API的IP地址添加到数据库防火墙设置中 这是我的API:C# Azure应用程序到Azure数据库“;“连接失败”;具有实体框架,c#,sql,.net,entity-framework,azure,C#,Sql,.net,Entity Framework,Azure,我有一个C#NetWeb API部署到Azure应用程序服务,我还有一个Azure SQL数据库 在API中,我使用实体框架插入到数据库中,但我不断收到错误消息:“基础提供程序在打开时失败” (在本地运行API(在调试模式下)并连接到本地数据库时,它工作正常)。 这可能是Azure数据库的权限/防火墙配置问题,还是其他问题 我已将当前IP地址添加到“Azure Set Server Firewall”中,是否需要将Azure Web API的IP地址添加到数据库防火墙设置中 这是我的API: p
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" />
我已经检查了您的连接字符串,它似乎是正确的。所以我认为您的问题可能是由于SQLAzure的模型发生了更改,但您的项目没有更新它。这是我在我这边复制的同一个问题: 我建议你更新你的模型 我是否需要将Azure Web API的IP地址添加到数据库防火墙设置中 我们可以在SQL Azure防火墙设置中将允许访问Azure服务设置为ON。因此,我们不需要添加Azure web API的地址
您需要从连接字符串中删除
integratedsecurity=True
,因为您正在指定用户名和密码 您是否能够在本地运行,但仍然成功连接到Azure SQL实例?当您指定用户名和密码时,为什么连接字符串中有integrated security=True
?谢谢!我删除了“integratedsecurity=true”并添加了“persistensecurityinfo=true”。它现在连接成功:)@juunas你应该把这个贴出来作为答案,这样苔丝就可以把这个问题标记为已经回答了是的,我已经把它设置为真了,谢谢。改变连接字符串解决了我的问题。