简体   繁体   中英

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.

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. I can perform the EF migration when I set as startup project of my Web Api project.

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. So, I installed these packages

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

and have this class which read the connectionstring from 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. I can perform the EF migration when I Set as Startup Project of this Console Test project.

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')

I have copied the same connectringstring for both appsettings.json in Console Test project and Web Api project.

@viveknuna thanks for the pointer. I found out the issue already. In my web api project appsettings.json, I have "ConnectionStrings" but it actually detect connectionstring without "s", "ConnectionString"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM