Home > Back-end >  Deploying Web API to Azure - Entity Framework Core migrations connection string not being picked up
Deploying Web API to Azure - Entity Framework Core migrations connection string not being picked up

Time:06-17

I am trying to deploy my Web API to an Azure App Service using Visual Studio 2022, however when I try to apply the migrations, it is not discovering the connection string although it discovers the database context:

enter image description here

This is the code of my database context class:

namespace MyPortfolioWebAPI.Data
{
    public class MyPortfolioContext:DbContext
    {
        public MyPortfolioContext(DbContextOptions<MyPortfolioContext> options) : base(options)
        {
        }

        public DbSet<Emails> Emails { get; set; } = null!;
        public DbSet<Projects> Projects { get; set; } = null!;
    }
}

This is my appsettings.json file:

enter image description here

It works fine on my local machine, however I am trying to publish it to Azure.

CodePudding user response:

Change your appsettings.json file like below. The issue should be fixed.

{
   "Logging": {
     "LogLevel": {
        "Default": "Information",
        "Microsoft.AspNetCore": "Warning"
     }
   },
   "ConnectionStrings": {
      "DefaultConnection": "Your_Connection_String"
   },
  "AllowedHosts": "*"
}

I test in my local and use .net6 project. And found builder.Configuration.GetConnectionString can directly identify the element under the key of ConnectionStrings.

Your code has one more layer of Data nested, that's why it can't be read.

  • Related