我有一个C#.Net Web API 部署到 Azure App Service ,我还有一个 Azure SQL数据库。
在API中我使用Entity Framework插入数据库,但我不断收到错误消息:" 基础提供程序在打开时失败"。
(在本地运行API(在调试模式下)连接到本地数据库时,它工作正常)。 这可能是Azure数据库的权限/防火墙配置问题,还是其他什么?
我已经在" Azure Set Server Firewall"中添加了我当前的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 Entity Framework中连接字符串的默认格式,我只添加了用户名,密码并更改了数据源和目录字段。它是否正确?
<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" />