0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thabiso
  • 67
  • 2
  • 8
  • am i suppose to add the connection string on apply this migration on publish manually or is it supposed to auto generate it like i thought – Thabiso Jun 14 '22 at 20:24
  • MayBe you can refer to this [issue](https://stackoverflow.com/questions/53936382/configure-my-connection-string-in-the-azure-application-setting-and-configure-it) – Xinran Shen Jun 15 '22 at 07:59
  • If my reply is helpful, please accept it as answer(click on the mark option beside the reply to toggle it from greyed out to fill in.), see https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Jason Pan Jun 16 '22 at 03:13

1 Answers1

0

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.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29