'ASP.NET Core: Error while Updating Migration (Database first approach)
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:
- SQL Server Version: 18.4
- .NET Core Version: 3.1.2
- I am trying with windows authentication
Solution 1:[1]
Try to change the connection string to: "DevConnection": "Server=.\\SQLEXPRESS;Database=Mydatabase:Trusted_Connection=true;MultipleActiveResultSets=true;"
. In this way it works for me!
Solution 2:[2]
Use for Database first Approach Entity Framework Core In ASP.NET Core MVC
Step 1:Add Packages in your project:
1.Add Microsoft.EntityFrameworkCore.Design
2.Add Microsoft.EntityFrameworkCore.SqlServer
3.Add Microsoft.EntityFrameworkCore.Tools
Step 2: Add Migration command in Package Manager Console:
Scaffold-DbContext "Server=Server name;Database=Database name;User
Id=User name;Password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | bertilvandekerkhove |
Solution 2 | Md Tanvir Asad |