[英]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,因此我认为问题不在我的连接字符串中
public class CommanderContext : DbContext
{
public CommanderContext(DbContextOptions<CommanderContext> opt) : base(opt)
{
}
public DbSet<Command> Commands { get; set; }
}
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CommanderConnection": "Server=localhost;Initial Catalog=CommanderDB,User ID=CommanderApi;Password=*****;"
}
}
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.