简体   繁体   English

尝试在 .NET Core 中更新数据库时出错

[英]Error when trying to update database in .NET Core

I am trying to update a database in .NET Core using the command我正在尝试使用以下命令更新 .NET Core 中的数据库

dotnet ef database update

but I am getting the following error但我收到以下错误

Error Number:18456,State:1,Class:14错误编号:18456,状态:1,类别:14
Login failed for user ''.用户 '' 登录失败。

I can log in in my SQL Server with this User ID and password so I don´t think the problem is in my connection string我可以使用此用户 ID 和密码登录我的 SQL Server,因此我认为问题不在我的连接字符串中

Context语境

   public class CommanderContext : DbContext
    {
        public CommanderContext(DbContextOptions<CommanderContext> opt) : base(opt)
        {

        }
        public DbSet<Command> Commands { get; set; }
    }
}

appsettings.cs appsettings.cs

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "CommanderConnection": "Server=localhost;Initial Catalog=CommanderDB,User ID=CommanderApi;Password=*****;"
  }
}

Startup.cs启动文件


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
            services.AddDbContext<CommanderContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("CommanderConnection")));
            services.AddControllers();
            services.AddScoped<ICommanderRepo, MockCommanderRepo>();


        }

I also leave a print of my SQL Server我还留下了我的 SQL Server 的打印件

在此处输入图片说明

Your issue is that you've got a comma in the connection string just before User Id.您的问题是您在用户 ID 之前的连接字符串中有一个逗号。

This needs to be a semicolon:这需要是一个分号:

 "CommanderConnection": "Server=localhost;Initial Catalog=CommanderDB;User ID=CommanderApi;Password=*****;"

"ConnectionStrings": { "CommanderConnection": "Data Source=localhost;Initial Catalog=CommanderDB;User ID=CommanderApi;Password=*****;" "ConnectionStrings": { "CommanderConnection": "Data Source=localhost;Initial Catalog=CommanderDB;User ID=CommanderApi;Password=*****;" } }

You can look in the SQL Server errorlog to see what the exact reason for the login failure is.您可以查看 SQL Server 错误日志以了解登录失败的确切原因。 I would guess that either the database name or the password is misspelled.我猜想是数据库名称或密码拼错了。

However, that is not the real error.然而,这并不是真正的错误。 The real error is that you use sa in the connection string.真正的错误是您在连接字符串中使用了 sa。 An application should never log in as sa!应用程序永远不应该以 sa 身份登录! An application should either run with the credentials of the actual user, or run under a login with limited permissions应用程序应该使用实际用户的凭据运行,或者在具有有限权限的登录名下运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 Web API Core 中的 EF Core 在启动时更新数据库时出现“无法访问已处理的对象”错误 - Getting "'Cannot access a disposed object" error when trying to update database on startup using EF Core in Web API Core 尝试更新数据库中的值时出错 - Error when trying to update values in database 尝试使用asp.net更新数据库时出现异常错误 - Exception Error in trying to update database using asp.net 尝试在 .NET CORE Web 应用程序中创建控制器时出错 - Error when trying to create a Controller in .NET CORE Web Application 尝试在 .net 核心 web api 中实现分页时出现 SqlException 错误 - SqlException error when trying to implement pagination in .net core web api 使用EF Core更新数据库时发生错误 - Error Happened when Update Database Using EF Core 在 .NET Core 中连接到 Oracle 数据库时出现 Kerberos 错误 - Kerberos error when connecting to Oracle database in .NET Core 更新数据库引发 FK 级联错误(EF Core / C# / ASP.NET Core) - Update-Database throwing FK cascade error (EF Core / C# / ASP.NET Core) EF-Core:表“名称”已存在 - 尝试更新数据库时 - EF-Core: Table "name" already exists - when trying to update database 无法在.NET Core中使用EF更新数据库 - Cannot Update-Database with EF in .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM