为什么我的ASP.NET Core SQL Server Express连接字符串出现错误? [英] Why am I getting an error in my ASP.NET Core SQL Server Express Connection String?

查看:447
本文介绍了为什么我的ASP.NET Core SQL Server Express连接字符串出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近启动了一个新的ASP.NET Core Web应用程序,并希望将其连接到本地SQLExpress数据库。我一直在关注文档,但是当它尝试在

I've recently started a new ASP.NET Core web application and I'd like to connect it to my local SQLExpress database. I've been following the documentation but I'm getting an error of "Value cannot be null" when it tries to read my connection string at

options.UseSqlServer(configuration.GetConnectionString( DefaultConnection)

到目前为止,这是我的代码。在 Startup中.cs 我具有以下设置:

Here is my code so far. In Startup.cs I have the following setup:

using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Blondie.CoreApiSql.Models;
using Microsoft.Extensions.Configuration;

namespace Blondie.CoreApiSql
{
    public class Startup
    {
        private readonly IConfiguration configuration;
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DatabaseContext>
                (options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
            services.AddMvc();
        }
        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }
    }
}

在运行应用程序时,错误在以下行触发:

When you run the appliation the error is triggered at the following line:

services.AddDbContext< DatabaseContext> (options => options.UseSqlServer(configuration.GetConnectionString( DefaultConnection))));

我已经在 appsettings.json 如下文档所述:

I have defined the connection string in the appsettings.json as described in the documentation which is as follows:

{
    "ConnectionStrings": {
        "DefaultConnection": "Data Source=DESKTOP-PC009\\SQLEXPRESS;Database=Senua;Trusted_Connection=True;MultipleActiveResultSets=true"
    },
    "Logging": {
      "IncludeScopes": false,
      "Debug": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "Console": {
        "LogLevel": {
          "Default": "Warning"
        }
      }
    }
  }

我已经创建了 Senua code>数据库中没有数据的表。我的数据库使用Windows身份验证。

I have already created the Senua database with a table in it which has no data. My database uses windows authentication.

我已通过以下方式用单个表定义了数据库上下文。

I have defined my database context in the following manner with my single table.

public class DatabaseContext : DbContext
{
    public DatabaseContext(DbContextOptions<DatabaseContext> options) 
        : base(options)
    {
    }
    public DbSet<Hellheim> Hellheim { get; set; }
}

据我了解,如果您已经未能在 appsettings.json 文件中定义您的连接字符串,但我已经添加了它,以前有人遇到过此问题吗?

It was my understanding that this error is only typically visibly if you've failed to define your connection string in the appsettings.json file but I've already added it, has anyone had this problem before?

推荐答案

定义一个值并将其分配给Configuration的 IConfiguration 并需要一个值。
在构造函数中定义依赖注入

Define and assign a value to Configuration its of IConfiguration and requires a value. Define the Dependency Injection in the constructor

public Startup(IConfiguration configuration)
    {
        this.configuration = configuration;
    }

这篇关于为什么我的ASP.NET Core SQL Server Express连接字符串出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆