我正在尝试使用 appsetting.json 中编写的连接字符串连接到数据库。我尝试将它传递到 AddDbContext 中的 UseSqlServer("...") 中,但它不起作用。当我在 Context 类中编写它时,它可以工作。因此,程序运行没有错误,但无法连接到数据库。
有人知道这里出了什么问题吗?下面是Program.cs代码:
using IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "Subscriber Service";
})
.ConfigureServices(services =>
{
services.AddHostedService<DeliveryService>()
.AddSingleton<IQueueService, QueueService>()
.AddSingleton<IMailTransport, MailTransport>()
.AddSingleton<IHttpTransport, HttpTransport>()
.AddDbContext<nbiot_core_svctestContext>(
options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
})
.Build();
await host.RunAsync();
Run Code Online (Sandbox Code Playgroud) c# connection-string entity-framework-core .net-6.0 worker-service
我可以以某种方式简化这个 switch 语句,因为这两种情况都只使用另一个函数参数做同样的事情吗?
\nswitch (data.Subscriber.Protocol)\n{\n case "email json":\n builder.Attachments.Add("O\xc4\x8ditanje.json", CraftAttachment(data));\n break;\n case "email text":\n builder.Attachments.Add("O\xc4\x8ditanje.txt", CraftAttachment(data));\n break;\n default:\n break;\n}\nRun Code Online (Sandbox Code Playgroud)\n