ASP.NET Core: Error while Updating Migration (Database first approach)

asp.net-core asp.net-web-api entity-framework entity-framework-core sql-server

Question

I have created an application in .NET Core where I am trying with database first approach.

I was able to add Migration using "Add-Migration InitialCreate" but when I am trying to create a database in SQL Server using " Update-Database" I am getting the below error.

Login failed for user ''.

Below is my content from appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DevConnection": "Server=(local)\\sqlexpress;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;"
  }
}

Just FYI Also I was facing issue to create migration so I have added below code after that I was able to create migrations.

public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
{
    public MyDbContext CreateDbContext(string[] args)
    {
        IConfigurationRoot configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json")
            .Build();
        var builder = new DbContextOptionsBuilder<MyDbContext>();
        var connectionString = configuration.GetConnectionString("DevConnection");
        builder.UseSqlServer(connectionString);
        return new MyDbContext(builder.Options);
    }
}

Notes:

  1. SQL Server Version: 18.4
  2. .NET Core Version: 3.1.2
  3. I am trying with windows authentication
1
0
3/11/2020 9:38:25 AM

Popular Answer

Try to change the connection string to: "DevConnection": "Server=.\\SQLEXPRESS;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;". In this way it works for me!

0
3/9/2020 8:20:56 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow