How to add connection string in asp.net core
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajesh GamiRank #30 · 41.5K reputationPosted Jan 3, 2023, 1:10 AM EST
1) https://www.c-sharpcorner.com/blogs/database-connections-in-asp-net-core-20
2) https://stackoverflow.com/questions/39083372/how-to-read-connection-string-in-net-core
Vishal JoshiRank #377 · 6.7K reputationPosted Jan 3, 2023, 1:22 AM EST
Hello Peter,
You can store the connection string in appsettings.json file.
{ "ConnectionStrings": { "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;" }, }You can get the connection string from JSON file using GetConnectionString() and setup in a startup.cs file as shown in the below code.
public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"))); }Thanks