This project shows how to use Entity Framework to connect a C# console app to the Pagila database.
-
Fork this repo on GitHub.
-
Go to Visual Studio to clone your forked repo:
Gitmenu ->Clone Repository- Under
Browse Repositories, click theGitHubbutton. - Select your
EFDemorepo and make a note of where the project is being saved. - Click
Clone.
-
Open the solution:
Filemenu ->Open->Project/Solution- Find where the project is saved, and open the
.slnxfile
-
Go to Solution Explorer in Visual Studio
- Right-click
EFDemo- Select
Manage Nuget Packages - Browse -> Search for these packages and install each one:
Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.JsonMicrosoft.EntityFrameworkCoreNpgsql.EntityFrameworkCore.PostgreSQL
- Select
- Right-click
EFDemo- Add -> New Item
- Name the file
appsettings.json
- Right-click
-
Paste this code in appsettings.json:
{ "ConnectionStrings": { "Pagila": "YOUR_CONNECTION_STRING_GOES_HERE" } } -
Replace
YOUR_CONNECTION_STRING_GOES_HEREwith your connection string, which will look something like this:Host=YOUR_SERVER_NAME_GOES_HERE;Port=5432;Database=pagila;Username=YOUR_USERNAME_GOES_HERE;Password=YOUR_PASSWORD_GOES_HERE -
Click F5 or the play button to run the code.