首页 > 解决方案 > 挂火后台作业给出 SQL 异常:登录失败

问题描述

我遵循的教程 - https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html

在启动类中配置服务

     public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IBackgroundJobClient backgroundJobs)
    {
    app.UseHangfireDashboard();
            backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!")); 
}

SqlException:无法打开登录请求的数据库“HangfireTest”。登录失败。用户“PENGUIN\PC”登录失败。

当我运行应用程序时出现此异常?

标签: sql-serverasp.net-coreasp.net-web-apihangfirehangfire-sql

解决方案


我可以通过更改 apsettings.json 文件中的连接字符串来解决这个问题,如果您需要找到连接字符串,请右键单击 Visual Studio 的 SQl 服务器对象资源管理器的数据库实例 - > 属性并获取连接字符串。此连接字符串应与 appsettings.json 一起放置

"ConnectionStrings": {
"ApplicationDbContext": "server=(localdb)\\mssqllocaldb;Database=ApplicationDbContext;Trusted_Connection=True;MultipleActiveResultSets=true",
"HangfireConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

推荐阅读