简体   繁体   中英

Config connection string in .net core 6

I'm attempting to connect to my ASP.NET Core Web API application (.NET 6 in Visual Studio 2022 Preview) with SQL Server. And I tried to use the following code to configure the connection string in the Startup class as I used to.

services.AddDbContext<DEMOWTSSPortalContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

But in .NET 6, I recognize that Startup and Program classes are merged into one class. And the above code is not usable in .NET 6. AddDbContext is not recognized. So do you have any idea or documentation about this update, and how to configure connection strings in .NET 6?

Configuration.GetConnectionString(string connName) in .NET6 is under builder.

var builder = WebApplication.CreateBuilder(args);
string  connString = builder.Configuration.GetConnectionString("DefaultConnection");

also AddDbContext is under builder.services.

builder.Services.AddDbContext<YourContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));

});

.Net 6 Simplifies a lot of a tasks and introduces WebApplicationBuilder which in turn gives you access to the new Configuration builder and Service Collection

var builder = WebApplication.CreateBuilder(args);

Properties

  • Configuration : A collection of configuration providers for the application to compose. This is useful for adding new configuration sources and providers.

  • Environment : Provides information about the web hosting environment an application is running.

  • Host : An IHostBuilder for configuring host specific properties, but not building. To build after configuration, call Build().

  • Logging : A collection of logging providers for the application to compose. This is useful for adding new logging providers.

  • Services : A collection of services for the application to compose. This is useful for adding user provided or framework provided services.

  • WebHost : An IWebHostBuilder for configuring server specific properties, but not building. To build after configuration, call Build().

To add a DbContext to the Di Container and configure it, there are many options however the most straightforward is

builder.Services.AddDbContext<YourContext>(options =>
{
   options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

Nugets packages

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer to use UseSqlServer

You can try to read in your controller like this..

private readonly IConfiguration _configuration;

    public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
    {
        _logger = logger;
       
       string _configuration = configuration.GetSection("connectionStrings").GetChildren().FirstOrDefault(config => config.Key == "Title").Value;

       
     
    }

NOTE: you can get value based on key provided above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM