title: Why won't my API's updated DbContext fetch a newly added DB column?
body: <p>I have an Azure SQL DB that initially had the following columns:</p>
  <p>user name
  password hash
  password salt</p>
  <p>This DB serves a .NET Core C# API that checks username and password to return a JWT token.</p>
  <p>The API had a User object that comprised all three columns with the correct types, a DbContext with a DbSet&lt;User&gt;, and an IServiceCollection that used said DbContext.</p>
  <p>The API worked fine, returning a JWT token as needed.</p>
  <p>I have since needed to add an extra parameter to check and pass to the JWT creation - the relevant column has been created in the DB, the User object in the API has been updated to include the extra parameter and that extra parameter is observed in the Intellisense throughout the API code.</p>
  <p>The issue is that when the API is deployed to Azure, the extra parameter isn't being recognised and populated; how do I make the API correctly update to use the new DbContext and retrieve the User with the extra parameter?</p>
  <p>(I've omitted the interfaces for brevity, as they're essentially the corresponding classes)</p>
  <p>User, UserRequest and MyApiDbContext Classes:</p>
  <pre><code>using Microsoft.EntityFrameworkCore;
  
  namespace MyApi.Models
  {
      // Basic user model used for authentication
      public class User
      {
          public string UserId { get; set; }
          public byte[] PasswordHash { get; set; }
          public byte[] PasswordSalt { get; set; }
          public string ExtraParam { get; set; } // newly added parameter
      }
  
      public class UserRequest
      {
          public string UserId { get; set; }
          public string password { get; set; }
      }
  
      public class MyApiDbContext : DbContext
      {
          public MyApiDbContext(DbContextOptions&lt;MyApiDbContext&gt; options)
              : base(options)
          {
          }
                  
          public DbSet&lt;User&gt; Users { get; set; }
      }
  }
  </code></pre>
  <p>The AuthRepository that retrieves the user:</p>
  <pre><code>using Microsoft.EntityFrameworkCore;
  using MyApi.Interfaces;
  using MyApi.Models;
  using System.Threading.Tasks;
  
  namespace MyApi.Services
  {
      public class AuthRepository : IAuthRepository
      {
          private readonly MyApiDbContext _context;
          public AuthRepository(MyApiDbContext context)
          {
              _context = context;
          }
          public async Task&lt;User&gt; Login(string username, string password)
          {
              // my test user gets returned
              User returnedUser = await _context.Users.FirstOrDefaultAsync(x =&gt; x.UserId == username);
  
              if (returnedUser == null)
              {
                  return null;
              }
  
              // the password get verified
              if (!VerifyPasswordHash(password, returnedUser.PasswordHash, returnedUser.PasswordSalt))
              {
                  return null;
              }
  
              // this does not get changed, but the value set in the DB is definitely a string
              if (returnedUser.ExtraParam == null || returnedUser.ExtraParam == &quot;&quot;)
              {
                  returnedUser.ExtraParam = &quot;placeholder&quot;
              }
  
              return returnedUser;
          }
      }
  }
  </code></pre>
  <p>The AuthService that calls the AuthRepository for the user then &quot;creates the JWT token&quot; (just returning a string for this example), currently set up to return the user details:</p>
  <pre><code>using Microsoft.Extensions.Options;
  using MyApi.Interfaces;
  using MyApi.Models;
  using System;
  using System.Threading.Tasks;
  
  namespace MyApi.Services
  {
      public class AuthService : IAuthService
      {
          private readonly IOptions&lt;MyApiBlobStorageOptions&gt; _settings;
          private readonly IAuthRepository _repository;
  
          public AuthService(IOptions&lt;MyApiBlobStorageOptions&gt; settings, IAuthRepository repository)
          {
              _repository = repository;
              _settings = settings;
          }
  
          public async Task&lt;string&gt; Login(string username, string password)
          {
              User returnedUser = await _repository.Login(username, password);
  
              if (returnedUser != null)
              {
                  // currently returns &quot;UserIdInDB,ProvidedPasswordFromLogin,&quot;
                  return $&quot;{returnedUser.UserId},{password},{returnedUser.ExtraParam}&quot;;
              }
  
              return null;
          }
      }
  }
  </code></pre>
  <p>The controller that calls the AuthService:</p>
  <pre><code>using Microsoft.AspNetCore.Mvc;
  using MyApi.Interfaces;
  using MyApi.Models;
  using System.Threading.Tasks;
  using Microsoft.Extensions.Logging;
  
  namespace MyApi.Controllers
  {
      [Route(&quot;api/[controller]&quot;)]
      [ApiController]
      public class AuthController : ControllerBase
      {
          private readonly MyApiDbContext _context;
          private readonly IAuthService _authService;
  
          public AuthController(MyApiDbContext context, IAuthService authService)
          {
              _context = context;
              _authService = authService;
          }
  
          [HttpPost(&quot;login&quot;)]
          public async Task&lt;IActionResult&gt; Login(UserRequest loginUser)
          {
              string token = await _authService.Login(loginUser.UserId, loginUser.Password);
  
              if (token != null)
              {
                  return Ok(token);
              }
  
              return Unauthorized(&quot;Access Denied!!&quot;);
          }
      }
  }
  </code></pre>
  <p>The startup class that registers everything:</p>
  <pre><code>using System.Text;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Authentication.JwtBearer;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Microsoft.Extensions.Logging;
  using Microsoft.IdentityModel.Tokens;
  using MyApi.Interfaces;
  using MyApi.Models;
  using MyApi.Services;
  using Microsoft.Extensions.Azure;
  using Azure.Storage.Queues;
  using Azure.Storage.Blobs;
  using Azure.Core.Extensions;
  using System;
  
  namespace MyApi
  {
      public class Startup
      {
          public IConfiguration Configuration { get; }
          private readonly ILogger&lt;Startup&gt; _logger;
          private readonly IConfiguration _config;
          public Startup(ILogger&lt;Startup&gt; logger, IConfiguration config)
          {
              _logger = logger;
              _config = config;
          }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              // Add dBContext for DB
              services.AddDbContextPool&lt;MyApiDbContext&gt;(options =&gt; options.UseSqlServer(_config.GetConnectionString(&quot;MyAzureDb&quot;)));
              
              // Add DI Reference for Repository
              services.AddScoped&lt;IAuthRepository, AuthRepository&gt;();
  
              // Add DI Reference for Azure Blob Storage Processes
              services.AddScoped&lt;IBlobService, AzureBlobService&gt;();
  
              // DI Reference for AuthService
              services.AddScoped&lt;IAuthService, AuthService&gt;();
  
              // Add configuration section for Constructor Injection
              services.Configure&lt;ApiBlobStorageOptions&gt;(_config.GetSection(&quot;MyApiBlobStorage&quot;));
  
              services.AddMvc(mvcOptions =&gt; mvcOptions.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Latest);
  
              services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                  .AddJwtBearer(options =&gt;
                  {
                      options.TokenValidationParameters = new TokenValidationParameters
                      {
                          ValidateIssuerSigningKey = true,
                          IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
                              .GetBytes(_config.GetSection(&quot;MyApiBlobStorage:Secret&quot;).Value)),
                          ValidateIssuer = false,
                          ValidateAudience = false
                      };
                      options.Events = new JwtBearerEvents()
                      {
                          OnAuthenticationFailed = context =&gt;
                              {
                                  _logger.LogWarning(&quot;Token authentication failed whilst attempting to upload file&quot;);
  
                                  return Task.CompletedTask;
                              }
                      };
                  });
              services.AddAzureClients(builder =&gt;
              {
                  builder.AddBlobServiceClient(Configuration[&quot;ConnectionStrings:MyApiBlobStorage/AzureBlobStorageConnectionString:blob&quot;], preferMsi: true);
              });
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
              else
              {
                  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                  app.UseHsts();
              }
  
              app.UseHttpsRedirection();
  
              app.UseCors(x =&gt; x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
              app.UseAuthentication();
              app.UseMvc();
          }
      }
      internal static class StartupExtensions
      {
          public static IAzureClientBuilder&lt;BlobServiceClient, BlobClientOptions&gt; AddBlobServiceClient(this AzureClientFactoryBuilder builder, string serviceUriOrConnectionString, bool preferMsi)
          {
              if (preferMsi &amp;&amp; Uri.TryCreate(serviceUriOrConnectionString, UriKind.Absolute, out Uri serviceUri))
              {
                  return builder.AddBlobServiceClient(serviceUri);
              }
              else
              {
                  return builder.AddBlobServiceClient(serviceUriOrConnectionString);
              }
          }
          public static IAzureClientBuilder&lt;QueueServiceClient, QueueClientOptions&gt; AddQueueServiceClient(this AzureClientFactoryBuilder builder, string serviceUriOrConnectionString, bool preferMsi)
          {
              if (preferMsi &amp;&amp; Uri.TryCreate(serviceUriOrConnectionString, UriKind.Absolute, out Uri serviceUri))
              {
                  return builder.AddQueueServiceClient(serviceUri);
              }
              else
              {
                  return builder.AddQueueServiceClient(serviceUriOrConnectionString);
              }
          }
      }
  }
  </code></pre>
  <p>Let me know if there is anything else required for understanding: the only difference between before and now is the addition of ExtraParam and the corresponding references throughout for the API, and the DB getting the identically named column.</p>
  <p>I tried adding the parameter and deploying it to Azure and making the POST request as normal, starting and stopping the app service, deploying the API while the app service was stopped and starting it again, and restarting the app service. I don't know how much I could try changing up what I'm doing, I'm trying to do exactly the same as before, but with an extra parameter getting requested from the DB.</p>
  <p>I can also confirm that the DB contains the ExtraParam column, and that it contains values against the existing data rows, as viewed using the Azure Portal's DB Query Editor.</p>