locked
Unable to retreive connection strings from appsettings.json in asp.net core application RRS feed

  • Question

  • in a simple example I have saved the following in an appsettings.json file for an asp.net core web api application

    {
      "ConnectionStrings": {
        "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
      }
    }

    In my startup.cs file I am testing to see if I can retrieve the code with the following :

     var connectionString = Configuration.GetConnectionString("BloggingDatabase");

    and it returns a null value

    ultimately I would like to accomplish something like this

    services.AddDbContext<BloggingContext>(options =>
                options.UseSqlServer(GetConnectionString("BloggingDatabase"))

    );


    Sorry for the delayed response.  I  found in my case that since it was a multi project solution .the connection string needed to be embedded in another project in order to work correctly
    • Edited by Congero Friday, August 28, 2020 4:34 PM
    Wednesday, August 12, 2020 3:29 PM

All replies

  • You can store it in app.config

    <appSettings>
       <add key="DatabaseServer" value=".\SQLEXPRESS" />
       <add key="NorthWinCatalog" value="NorthWind2020" />
    </appSettings>

    Add a reference to System.Configuration. Add the following using statement

    using static System.Configuration.ConfigurationManager;


    Then do a modified version of the following

    https://github.com/karenpayneoregon/NorthWind-2020/blob/master/North/Contexts/NorthwindContext.cs#L45


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Wednesday, August 12, 2020 3:55 PM
  • hi

    static class YourClass
    {
        public static IConfigurationRoot Configuration;
    
        public static string GetConnectionString()
        {
             var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json");
    
             Configuration = builder.Build();
             var connectionString = Configuration["ConnectionStrings:YourConnectionString"];
    
        }
    
    }

    Thanks and egards

    Wednesday, August 12, 2020 4:01 PM
  • You'll want to DI the connectionstring into the Dbcontext.

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

    https://hackernoon.com/asp-net-core-how-to-use-dependency-injection-in-entity-framework-core-4388fc5c148b

    The WebAPI project that has the Startup.cs is reading appsettings.json for the connectionstring  and using the connectionstring class that is is in the Entities classlib project that is DI into the PublishingContext in the Models folder in the DAL classlib project, wich is in the below example solution on Github.

    How to do it is simple, just flow the code.

    https://github.com/darnold924/PublishingCompany

    ASP.NET Core can be discussed in the ASP.NET forums.

    https://forums.asp.net/



    • Edited by DA924x Wednesday, August 12, 2020 4:39 PM
    Wednesday, August 12, 2020 4:36 PM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 28, 2020 8:03 AM