Difficulties connecting to database through Scaffold-DbContext

1.1k Views Asked by At

I am learning ASP.NET core 6 Multitier architecture. I tried making connection to database but I keep getting this error:

Error Number:4060,State:1,Class:11
Cannot open database "Blogging" requested by the login. 
The login failed.
Login failed for user 'TECHRITOMA\TECHRITOMA Inc'.

I tried using the command :

 Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

and i was expecting the model class to be created.

1

There are 1 best solutions below

0
On

As Sydney_dev said. First, Visual studio needs to successfully connect to the local database. Official document: SQL Server Express LocalDB.

.NET Core+EF Scaffold-DbContext command uses.

Scaffold-DbContext "Data Source=ip address;Initial Catalog=database name;User ID=account;password=password;" -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models;

Scaffold-DbContext "Data Source=ip address;Initial Catalog=database name;User ID=account;password=password;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables "Blog","Post" -ContextDir Context -Context BlogContext - ContextNamespace New.Namespace

Official document.