简体   繁体   English

无法为 .NET Core Class Lib 项目和 .NET Core Web Api 项目执行 EF Core。 错误:值不能为 null。(参数“connectionString”)

[英]Could not perform EF Core for .NET Core Class Lib Project and .NET Core Web Api Project. Error: Value cannot be null. (Parameter 'connectionString')

I split out my solution into multiple projects / layers.我将我的解决方案分成多个项目/层。 The technology which I am using is C# in .NET Core and Entity Framework Core.我使用的技术是 .NET Core 和 Entity Framework Core 中的 C#。

I have Infrastructure project which is a .NET Core class library project which I put the DataContext class. If I put my connection string in the DataContext class directly, it works.我有一个基础设施项目,它是一个 .NET 核心 class 库项目,我将 DataContext class 放入其中。如果我将连接字符串直接放入DataContext class,它就可以工作。 I can perform the EF migration when I set as startup project of my Web Api project.当我set as startup project时,我可以执行 EF 迁移。

public class DataContext : DbContext
{
    const string connectionString = "Server=STEVENGAI\\SQLEXPRESS;Database=CinemaDbNETCore;Trusted_Connection=True;MultipleActiveResultSets=true";

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        AppConfiguration appconfig = new AppConfiguration();
        optionsBuilder.UseSqlServer(appconfig.ConnectionString);
    }

    public DbSet<Movie> Movies { get; set; }
}

But, I need to put the connectionstring at appsettings.json instead.但是,我需要将连接字符串放在 appsettings.json 中。 So, I installed these packages所以,我安装了这些包

  • Microsoft.Extensions.Configuration Microsoft.Extensions.Configuration
  • Microsoft.Extensions.Configuration.Abstractions Microsoft.Extensions.Configuration.Abstractions
  • Microsoft.Extensions.Configuration.Json Microsoft.Extensions.Configuration.Json

and have this class which read the connectionstring from appsettings.json.并让这个 class 从 appsettings.json 读取连接字符串。

namespace CinemaApp.NETCore.Infrastructure
{
public class AppConfiguration
{
    public readonly string _connectionString = string.Empty;
    public AppConfiguration()
    {
        var configurationBuilder = new ConfigurationBuilder();
        var path = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
        configurationBuilder.AddJsonFile(path, false);

        var root = configurationBuilder.Build();
        _connectionString = root.GetSection("ConnectionString").GetSection("CinemaDb").Value;
        //var appSetting = root.GetSection("ApplicationSettings");
    }
    public string ConnectionString
    {
        get => _connectionString;
    }

}
}

Then, I create another .NET Core console project to test if I could get the connection string which it did successfully.然后,我创建另一个 .NET Core 控制台项目来测试我是否可以成功获取连接字符串。 I can perform the EF migration when I Set as Startup Project of this Console Test project.当我Set as Startup Project时,我可以执行 EF 迁移。

namespace ConsoleTest
{
class Program
{
    static void Main(string[] args)
    {
        AppConfiguration appconfig = new AppConfiguration();
        Console.WriteLine(appconfig.ConnectionString);
        Console.WriteLine("Hello World!");
    }
}
}

But, the problem is when I switch back Set as Startup Project to Web Api project, and perform EF migration, it gives me this error Value cannot be null. (Parameter 'connectionString')但是,问题是当我切换回Set as Startup Project到 Web Api 项目,并执行 EF 迁移时,它给我这个错误Value cannot be null. (Parameter 'connectionString')

I have copied the same connectringstring for both appsettings.json in Console Test project and Web Api project.我为控制台测试项目中的 appsettings.json 和 Web Api 项目复制了相同的连接字符串。

@viveknuna thanks for the pointer. @viveknuna 感谢您的指点。 I found out the issue already.我已经发现了这个问题。 In my web api project appsettings.json, I have "ConnectionStrings" but it actually detect connectionstring without "s", "ConnectionString"在我的 web api 项目 appsettings.json 中,我有"ConnectionStrings" ,但它实际上检测到没有“s”、 "ConnectionString"的连接字符串

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.ArgumentNullException:值不能为 null。(参数“connectionString”)asp.net 核心 Docker-compose - System.ArgumentNullException: Value cannot be null. (Parameter 'connectionString') asp.net Core Docker-compose Asp.Net Core 2.0 ArgumentNullException:值不能为null。 参数名称:connectionString - Asp.Net Core 2.0 ArgumentNullException: Value cannot be null. Parameter name: connectionString .NET 核心工作者异常:值不能是 null。 参数名称连接字符串 - .NET Core worker exception: Value cannot be null. Parameter name connectionString 当我在 asp.net core 3.1 中工作时收到此错误值不能为空。 (参数&#39;connectionString&#39;) - While I'm working ins asp.net core 3.1 getting this error Value cannot be null. (Parameter 'connectionString') Entity Framework Core 和 EF6 - dotnet ef 迁移添加 InitialCreate - 值不能为空。 (参数“连接字符串”) - Entity Framework Core and EF6 - dotnet ef migrations add InitialCreate - Value cannot be null. (Parameter 'connectionString') 值不能为空。 尝试使用 JsonConverter .net core 3.1 反序列化对象时出现(参数“值”)错误 - Value cannot be null. (Parameter 'value') error when trying to DeserializeObject using JsonConverter .net core 3.1 EF Core - 值不能为空。 (参数&#39;键&#39;) - EF Core - Value cannot be null. (Parameter 'key') 找不到 .NET 核心项目。 未生成资产 - Could not locate .NET Core project. Assets were not generated EF Core 6 错误“System.ArgumentNullException: Value cannot be null. (Parameter 'key')” using API method POST and PUT - EF Core 6 error "System.ArgumentNullException: Value cannot be null. (Parameter 'key')" using API method POST and PUT SqlNullValueException:数据为 Null。无法在 ASP.NET Core 6 MVC 项目中的 Null 值错误上调用此方法或属性 - SqlNullValueException: Data is Null. This method or property cannot be called on Null values error in ASP.NET Core 6 MVC project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM