Reputation: 45
I created a REST API using ASP.NET Web API, Entity Framework and a SQL database hosted on Microsoft Azure. I used Code first migration.
Here is the User Model class
namespace Trivio.Models
{
public partial class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
}
DbContext defined under Trivio.Models.ApplicationDbContext.cs
namespace Trivio.Models
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<User> UserInformation { get; set; }
public DbSet<Trivia> Trivia { get; set; }
public DbSet<TriviaHistoryModel> TriviaHistoryModel { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
First I created migrations, locally on a localDB. Then I updated my server code to Azure and enabled code first migrations. I know this works because my Azure SQL database populated with correct data. To test if my database was hosted correctly, I updated the connectionString to connect to the Azure SQL database
Here is the connection string under Web.config. (I have replaced password and username with dummy data)
<connectionStrings>
<add name="DefaultConnection" connectionString="Server=tcp:trivioserver.database.windows.net,1433;Initial Catalog=Trivio;Persist Security Info=False;User ID={mycorrectusername};Password={mypassword};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" providerName="System.Data.SqlClient"/>
</connectionStrings>
My user API controller under Trivio.Controllers.UserController.cs:
namespace Trivio.Controllers
{
[RoutePrefix("Trivia")]
public class UserController : ApiController
{
private ApplicationDbContext _context;
public UserController()
{
_context = new ApplicationDbContext();
}
//------------Routes---------------------------------------------
//Return List of Users
[HttpGet]
[Route("Users")]
public IEnumerable<User> GetUsers()
{
return _context.UserInformation.ToList();
}
}
}
Here's where the problem arises. When I run the code on my local server and make a GET call to localserver/Trivia/Users correct list of users from my Azure SQL database is returned.
However, After publishing to Azure, the exact same code returns an empty list of Users. I made a GET call to http://{myappname}.azurewebsites.net/Trivia/Users.
<ArrayOfUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Trivio.Models"/>
I know the code is correctly hosted because html code is loaded.
My ServiceConfigurationCloud file under Trivio.Azure
<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="Trivio.Azure" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2015-04.2.6">
<Role name="Trivio">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.
" value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>
I know that the database is not empty, because I manually checked it and the local server code connected to same database returns correct information.
Upvotes: 0
Views: 839
Reputation: 18387
I'm with Steve G on that. I think the easy way to identify / fix this problem is use the remote debugger and inspect what is wrong:
https://learn.microsoft.com/en-us/azure/app-service-web/web-sites-dotnet-troubleshoot-visual-studio
Upvotes: 1