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:
Try to change the connection string to: "DevConnection": "Server=.\\SQLEXPRESS;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;"
. In this way it works for me!