0

I am trying to update a database in .NET Core using the command

dotnet ef database update

but I am getting the following error

Error Number:18456,State:1,Class: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

Context

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

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

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

enter image description here

5
  • Is this a new or an existing database? Are remote connections allowed? Did this user ID/password work when you first scaffolded the database, if you're using Code-First approach? Commented Oct 13, 2020 at 21:24
  • The SSMS screenshot is not helpful at all, as it only shows existence of the SQL Login, not the rights Commented Oct 13, 2020 at 21:27
  • The clue is in the error message Login failed for user and your statement "I don´t think the problem is in my connection string" - the update statement may not be seeing the connection string or the string has errors. Commented Oct 13, 2020 at 21:27
  • @CoolBots it is a new database Commented Oct 13, 2020 at 21:31
  • @PeterSmith so my problem is in Startup.cs? Commented Oct 13, 2020 at 21:32

1 Answer 1

3

Your issue is that you've got a comma in the connection string just before User Id.

This needs to be a semicolon:

 "CommanderConnection": "Server=localhost;Initial Catalog=CommanderDB;User ID=CommanderApi;Password=*****;"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.