[英]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
所以,我安装了这些包
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.