简体   繁体   中英

Cannot resolve symbol GetConnectionstring

I have a .NET Core 3.0 class library. It is my first time working with .NET Core

I want to connect to a database.

https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings shows exactly how to do it.

https://learn.microsoft.com/en-us/do.net/api/microsoft.extensions.configuration.configurationextensions.getconnectionstring?view=do.net-plat-ext-3.1 shows the method which exists in the Microsoft.Extensions.Configuration namespace

I can't access it.

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;  //no underlines and imported via Nuget

namespace Core.Cms.DAL
{
    public class Entities : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {  optionsBuilder.UseSqlServer(Microsoft.Extensions.Configuration.GetConnectionString(ConnectionString.SqlServerExpress));

        }
    }
}

Screenshot

在此处输入图像描述

Why do I not have the Microsoft.Extensions.Configuration.GetConnectionString() method available?

edit

Restarted rider - same issue

Entities.cs(17, 45): [CS0234] The type or namespace name 'GetConnectionString' does not exist in the namespace 'Microsoft.Extensions.Configuration' (are you missing an assembly reference?)

You should use the IConfiguration interface. you can access to configuration form DI like this

private readonly IConfiguration _configuration;
public Entities(IConfiguration configuration)
{
    _configuration = configuration
}

then use the _configuration.GetConnectionString(ConnectionString.SqlServerExpress) instead of Microsoft.Extensions.Configuration.GetConnectionString()

optionsBuilder.UseSqlServer(_configuration.GetConnectionString(ConnectionString.SqlServerExpress));

if you are using the Asp.Net Core Web Application, This is a better way to config DbContext in startup class

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}
public IConfiguration Configuration { get; set; }
services.AddDbContext<Entities>(options =>
{
    options.UseSqlServer(Configuration.GetConnectionString(ConnectionString.SqlServerExpress));
});

Update

if you don't use the DI you can access to IConfiguration manually.

first you must install this packages

  • Microsoft.Extensions.Configuration.Json

  • Microsoft.Extensions.Configuration.FileExtensions

then build the IConfiguration

IConfiguration configuration = new ConfigurationBuilder()
   .AddJsonFile("appsettings.json", true,true)
   .Build();
optionsBuilder.UseSqlServer(configuration.GetConnectionString(ConnectionString.SqlServerExpress));

> - First :using Some of The Namespaces in startup.cs File

1-using Microsoft.EntityFrameworkCore;


2-using Microsoft.Extensions.Configuration;

> -Second : Write These Code inside Startup.cs file

Creating Property

public IConfiguration Configuration { get; set; }


2- And This ... After Properties..

 public Startup(IConfiguration configuration) { Configuration = configuration; }

In .NET 6 you now use builder.Configuration.GetConnectionString like this

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

Just Add blow code connection string in.Net 6

IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json, true, true").Build();
builder.Services.AddDbContext<DataContext>(options => options.UseNpgsql(configuration.GetConnectionString("DefaultConnection")));

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