Entity Framework Core Add Migration给出的值不能为空。(参数'连接字符串')

问题描述 投票:0回答:1

我是.NET Core的新手,正在通过Udemy的课程进行工作。我试图启用迁移,以做代码优先,但我一直得到一个错误的

值不能为空。(参数'连接字符串')

我做了一些研究,好像大部分时间都是拼写错误的。ConnectionStringsappsetttings.json 文件。但是,我把我的都看了一遍,还试了几次复制粘贴,以确保我的拼写是正确的。要不就是别的东西,要不就是我看了太多,忽略了什么。

Appsettings.json

{
    "ConnectionStings": {
        "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*"
}

启动.cs

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

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DbConnection")));
    services.AddControllers();
    services.AddAutoMapper(typeof(Startup));
    services.AddScoped<ICharacterService, CharacterService>();
}
c# asp.net-core entity-framework-core ef-code-first
1个回答
1
投票

在你的例子中,你拼错了 连接字符串

 {
        "ConnectionStings": {
            "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
        },
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },
        "AllowedHosts": "*"
    }

0
投票

通过应用设置和配置服务方法,看起来不错。

  "ConnectionStrings": {
    "DbContext": "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=dotnet-rpg;Integrated Security=SSPI; MultipleActiveResultSets=true;",
    "Logging": {
      "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    }
  }

不同的是,连接字符串中没有 "数据源"

在配置服务中

services.AddDbContextPool<DataContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DbContext"))
            );

请告诉我这是否解决了你的问题。

下面两个参考链接有详细的实现方法。

使用Entity Framework Core实现创建、读取、更新和删除功能。

使用与2019年配置Azure SQL关系型数据库。

© www.soinside.com 2019 - 2024. All rights reserved.