【发布时间】:2022-06-16 04:07:45
【问题描述】:
我是 Asp.Net Core 和 EF 的新手。我正在从数据库端开发一个简单的 CRUD,使用 Secrets.json 文件隐藏我的连接字符串凭据。
但我不知道如何使用 AddDbContext() 引用文件。
到目前为止我的代码:
public class Startup
{
public Startup(IConfigurationRoot configuration)
{
Configuration = configuration;
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<POTS.myDBContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("myConxStr")));
services.AddControllers();
}
代码运行时,AddDbContext<> 行出现此错误
System.ArgumentNullException HResult=0x80004003 消息=值 不能为空。 (参数'connectionString')
Source=Microsoft.EntityFrameworkCore.SqlServer StackTrace: 等等等等
我认为这是因为代码正在寻找appsettings.json 文件中的参数,我不希望连接字符串在该位置。
我错过了什么?
【问题讨论】:
-
你确定是 ASP.NET Core 6 而不是 5?您在项目中使用 Startup.cs。
-
@Rena 基于教程,我自己手动添加了 setup.cs。我知道不需要。还在学习中。
-
嗨@Fandango68,知道了。无论如何,我在下面分享了两种情况。你可以检查一下。
标签: entity-framework asp.net-core connection-string secretsmanager