【问题标题】:{"Value cannot be null. (Parameter 'connectionString')"} System.ArgumentNullException in Blazor Webassembly project{“值不能为空。(参数'connectionString')”} Blazor Webassembly 项目中的 System.ArgumentNullException
【发布时间】:2021-12-31 19:38:26
【问题描述】:

在我的项目中,connectionString 在运行时抛出空指针异常,即使配置正确。

appsettings.json

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-<DATABASE_NAME>;Trusted_Connection=True;MultipleActiveResultSets=true",
    "CustomConnection": "Server=(localdb)\\mssqllocaldb;Database=<DATABASE_NAME>"
  }

Startup.cs

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

我会在这里遗漏什么? 提前致谢。

ApplicationDbContext.cs

using IdentityServer4.EntityFramework.Options;
using Microsoft.AspNetCore.ApiAuthorization.IdentityServer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using App.Models;
using App.Shared;

namespace App.Server.Data
{
    public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
    {
        public ApplicationDbContext(
            DbContextOptions options,
            IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
        {

        }
        
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.ApplyConfiguration(new RoleConfiguration());
        }
    }

}

【问题讨论】:

  • 您能否显示 ApplicationDbContext pls 的受保护的覆盖无效 OnConfiguring(DbContextOptionsBuilder optionsBuilder) 方法?
  • @Serge 我用 ApplicationDbContext.cs 内容更新了问题。
  • 你有非标准的 costructor 和 OnModelCreating ,这就是问题所在。
  • @Serge 您能否详细说明或参考链接以更正此问题。我对 asp.net 和 blazor 有点陌生。
  • 您将不得不等待,也许有人可以帮助您。对我来说,做某事的信息不够,而且我的电脑上没有 blazor

标签: asp.net asp.net-mvc asp.net-core blazor-server-side blazor-webassembly


【解决方案1】:

不要使用 EntityFrameworkCore 提供的 AddDbContext 扩展方法,它需要 DbContext 上的标准构造函数,而是直接使用 AddScoped 并提供一个使用自定义参数返回构造的 DbContext 的方法:

变化:

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

收件人:

IOptions<OperationalStoreOptions> osOptions;

services.AddScoped<ApplicationDbContext>(sp =>
{
    DbContextOptionsBuilder<ApplicationDbContext> optsBuilder =
        new DbContextOptionsBuilder<ApplicationDbContext>()
            .UseSqlServer(Configuration.GetConnectionString("DefaultConnection");

    // If an instance of osOptions is available at this time
    return new ApplicationDbContext(optsBuilder.Options, osOptions);    

    // Or, if the options are registered in the service container
    // then resolve from the service provider
    return new ApplicationDbContext(
        optsBuilder.Options, 
        sp.GetRequiredService<IOptions<OperationalStoreOptions>());

};

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2021-07-02
    • 2018-06-03
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多