簡體   English   中英

connectionString不能為空值

[英]connectionString Can not be null value

我像往常一樣位於json配置文件中的連接字符串有問題:

  1. 我創建了一個類:applicationDbcontext
  2. 我做了一個DbSet {get; set;}
  3. 將其集成到控制器中
  4. 將其添加到服務

我已閱讀並嘗試插入相關問題中的所有建議: asp.net core 2.0-值不能為null。 參數名稱:connectionString但它們都不適合我這是我的實現,希望有人可以幫助我,謝謝

A_ DB上下文:

   public class ApplicationDbContext:DbContext
    {
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {
            Database.EnsureCreated();
        }
        public DbSet<Person> Persons { get; set; }
    }
}

B_應用程序設置:config.json和config.development.json

{
    "ConnectionString": {
        "testConnection": "Server=(localdb)\\mssqllocaldb;Database=mvc_db;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "AllowedHosts": "*"

}

C_服務注入

啟動 :

  public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.Json")
                .AddJsonFile("appsettings.Development.Json", true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

配置服務:

public void ConfigureServices(IServiceCollection services)
        {

            services.AddDbContext<ApplicationDbContext>(options => {
                options.UseSqlServer(Configuration.GetConnectionString("testConnection"));
            });
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

D_注入控制器:

 private readonly ApplicationDbContext _db;
        public PersonController(ApplicationDbContext db)
        {
            _db = db;
        }

        IEnumerable<Person> People { get; set; }
        public IActionResult Index()
        {
            People = _db.Persons.ToList();
            return View(People);
        }

我試圖將整個connectionString插入configuration.GetConnectionString(“ Here”);中; 並將連接字符串的位置從上向下更改,反之亦然。 但是沒有任何方法可以解決connectionString返回空值的問題。 任何幫助,請謝謝 在此處輸入圖片說明

對connectionString使用此結構

 {

 "ConnectionStrings": {
  "DefaultConnection": "xxx"
   }
}

檢查你錯過了JSON文件s應該ConnectionStrings

並以這種簡單的方式訪問您的連接字符串

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

在您的情況下, DefaultConnection應該是testConnection

我試圖將整個connectionString插入configuration.GetConnectionString(“ Here”);中;

但是您是否嘗試更改:

options.UseSqlServer(Configuration.GetConnectionString("testConnection"));

options.UseSqlServer("your connection string");

另外,您提到您有config.jsonconfig.development.json ,但是它們應該是appsettings.jsonappsettings.development.json

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM