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.
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.
Related
"Unable to find provider assembly 'Oracle.ManagedDataAccess.Core'. Ensure the name is correct and it's referenced by the project".
Dont Know why this error is comming while scaffolding.
Scaffold command i used.
Scaffold-DbContext -Provider Oracle.ManagedDataAccess.Core -Connection "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=18.88..)(PORT=11))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=CC)));User Id=CC;Password=C**8;" -OutputDir Models .
These are the package reference in my project
I have to create a layered .net core application including a database. I want to build the database entities with sql-first approach. The solution looks like this:
All of the projects are class libaries, except the FoodSupplementCompany.Program.
My question is, how can I use Scaffold-DbContext to generate the entities to the FoodSupplementCompany.Data project?
You can use the Package Manager Console:
cd .\FoodSupplementCompany.Data
dotnet ef dbcontext scaffold -s ..\FoodSupplementCompany.Program
The line cd .\FoodSupplementCompany.Data is for navigating into project directory where the DBContext is located.
And the line -s ..\FoodSupplementCompany.Program is for stating where is the startup project to use.
As the guide here states: scaffold-dbcontext
Example:
Scaffold-DbContext
"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer
-OutputDir Models
-OutputDir -> The directory to put files in. Paths are relative to the project directory.
enter image description hereI am experience this issue on my Nuget console, while running this command below. Please help me further thanks.
scaffold-DBContext "Data Source=(LocalDb)\MSSQLLocalDB; Catalog=eNtsaOnlineRegistrationDB;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer-OutputDir Models
Please check you scaffold command.
You can use -ContextDir (PMC) and --context-dir (CLI) to scaffold the DbContext class into a separate directory from the entity type classes.
Try following command in PMC.
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer -ContextDir Data -OutputDir Models
Also check your Models folder in project root directory with all folder access [read/edit] rights.check single space -OutputDir Models
You can find more detail on this link Reverse Engineering (Scaffolding)
Check you code especially for space characters etc. you have to put a space character before -OutputDir word...
Your code should be like this:
scaffold-DBContext "Data Source=(LocalDb)\MSSQLLocalDB; Catalog=eNtsaOnlineRegistrationDB;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
I am using the updated Nuget Package Manager Console in Visual Studio to scaffold models from a database I've connected from named Sail. When I enter the connection string into the Package manager console.
PM> Data Source=localhost;Initial Catalog=Sail;Integrated Security=True
I receive the following errors:
At line:1 char:12
+ Data Source=localhost;Initial Catalog=Sail;Integrated Security=True; ...
The Data section is missing its statement block.
Found my answer:The block below successfully generates models from the connected "Sail" SQL database.
Scaffold-DbContext –Connection "Server(localdb)\mssqllocaldb;Database=Sail;Trusted_Connection=True;"
(localdb)\mssqllocaldb;Database=Sail;Trusted_Connection=True;"
-Provider "Microsoft.EntityFrameworkCore.SqlServer" -OutputDir "Models" –Context "Sail" –Verbose -Force
Code First Environment
I'm trying to update the database from package Manager console. If my domain class changes, I have to drop and create the database. Instead of dropping the database, how can I update the database?
Commands
By using this command, I installed the Entity Framework successfully.
PM> Install-Package EntityFramework
By using this command, it created the Migration file in my project.
PM> Enable-Migrations
By using this command, I may update the table but I have a problem here.
PM> Update-Database
Error
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
Doubt is here
Sometimes it may update if only one field changes in POCO Class. For example I have updated the more number of Domain class. How can I update the database from Package manager Console?
You can specify connection string via ConnectionString parameter:
Update-Database -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Also you need to use this parameter with the same value for Add-Migration command:
Add-Migration Version_Name -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
I used
Enable-Migrations -EnableAutomaticMigrations -Force
then
Update-Database
And this migrated my changes (additional columns) while preserving my test data. I think this is the laziest option while prototyping.
Looks like you have multiple issues. Regarding not wanting to drop and recreate the database, that is determined by your database initializer. If you want to use migrations you change it to MigrateDatabaseToLatestVersion. http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm
Second, it doesn't matter how many fields you change, they will be rolled into a single migration based on changes from the last migration.
Third, as the others have pointed out, it seems you have a connection string issue. While you can add that to Add-Migration and Update-Migration I would probably fix it in the application. I set mine in the constructor of my context which points to my config file (web.config for ASP.NET).
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext()
: base("MyConnection", throwIfV1Schema: false)
{
Database.SetInitializer<ApplicationDbContext>(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MyObjextContextMigration>());
}
...
Where to put the connection string?
You do not need to specify it with every command in the package manager console. You can put it in appsettings.json in the project where your DbContext class (read "from DbContext derived class") resides.
{
"ConnectionStrings": {
"MyConnectionString": "Server=yourServer;Port=5432;Database=yourDatabase;User Id=yourDatabaseUsername;Password=yourDatabasePassword;"
}
}
It will be used for migrations.
Important: If you have multiple projects in your solution, you must select the project in the 'Default project'-dropdown (in the Package manager Console) and you must set the project as your startup project (in the Solution Explorer).
Failing to do so, might cause the wrong appsettings.json to be used with an incorrect/different connectionstring.
This was my experience with EF Core 2.1 and probably applies to the other versions of EF.
You need to update SSDT
go to tools> extension and updates > updates > SQL server data tools