{"id":20625122,"url":"https://github.com/jasonsturges/postgresql-dotnet-core","last_synced_at":"2025-04-15T15:04:50.034Z","repository":{"id":42501540,"uuid":"89875317","full_name":"jasonsturges/postgresql-dotnet-core","owner":"jasonsturges","description":"ASP.NET 7.0 Web Application using PostgreSQL with Entity Framework","archived":false,"fork":false,"pushed_at":"2022-12-29T21:03:54.000Z","size":1997,"stargazers_count":116,"open_issues_count":0,"forks_count":54,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-15T15:04:42.731Z","etag":null,"topics":["asp-net-core","aspnet","aspnetcore","database","dotnet","dotnet-core","entity-framework","postgresql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasonsturges.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-30T20:29:08.000Z","updated_at":"2025-01-10T04:14:21.000Z","dependencies_parsed_at":"2023-01-31T10:01:14.322Z","dependency_job_id":null,"html_url":"https://github.com/jasonsturges/postgresql-dotnet-core","commit_stats":null,"previous_names":[],"tags_count":4,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fpostgresql-dotnet-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fpostgresql-dotnet-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fpostgresql-dotnet-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasonsturges%2Fpostgresql-dotnet-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasonsturges","download_url":"https://codeload.github.com/jasonsturges/postgresql-dotnet-core/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249094932,"owners_count":21211837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","aspnet","aspnetcore","database","dotnet","dotnet-core","entity-framework","postgresql"],"created_at":"2024-11-16T13:08:14.330Z","updated_at":"2025-04-15T15:04:50.019Z","avatar_url":"https://github.com/jasonsturges.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PostgreSQL ASP.NET 7\n\nConvert an ASP.NET Core Web Application project to use PostgreSQL with Entity Framework.\n\nThis enables development of ASP.NET Core projects using [VS Code](https://code.visualstudio.com/) on macOS or linux targets.\n\nThis project uses [.NET 7.0](https://dotnet.microsoft.com/en-us/download/dotnet/7.0) target framework, ASP.NET Core Web Application MVC project scaffold from Visual Studio 2022 (version 17.4).\n\n![vscode](https://user-images.githubusercontent.com/1213591/210010019-e4b11daf-03df-41b6-b44c-368f0cd3cfde.png)\n\nProject setup has already been completed in this repository - assure [environment setup](#environment-setup); then, jump to [running the solution](#running-the-solution).\n\n\n## Environment Setup\n\nThis project requires PostgreSQL - installation instructions are provided below.\n\nIf using Visual Studio Code, you will need to generate ASP.NET Core developer certificates by issuing the following commands from a terminal:\n\n    dotnet dev-certs https --clean\n    dotnet dev-certs https\n\nFor command line `database ef` commands, you will need to install Entity Framework Core tools .NET CLI:\n\n    dotnet tool install --global dotnet-ef\n\n\n## Project Setup\n\n\nBelow, instructions are referenced to use PostgreSQL in a ASP.NET Core project.\n\n\n### Install NuGet packages\n\nInstall the `Npgsql.EntityFrameworkCore.PostgreSQL` NuGet package in the ASP.NET web application.\n\nTo do this, you can use the `dotnet` command line by executing:\n\n    $ dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL --version 3.1.2\n\nOr, edit the project's .csproj file and add the following line in the `PackageReference` item group:\n\n```xml\n\u003cPackageReference Include=\"Npgsql.EntityFrameworkCore.PostgreSQL\" Version=\"3.1.2\" /\u003e\n```\n\n\n### Update appsettings.json\n\nConfigure connection string in project's appsettings.json, replacing the `username`, `password`, and `dbname` appropriately:\n\n```json\n\"ConnectionStrings\": {\n    \"DefaultConnection\": \"User ID=username;Password=password;Server=localhost;Port=5432;Database=dbname;Integrated Security=true;Pooling=true;\"\n},\n```\n\n\n### Modify Program.cs\n\nInside Program.cs replace the `UseSqlServer` options with `UseNpgsql`:\n\n```cs\nbuilder.Services.AddDbContext\u003cApplicationDbContext\u003e(options =\u003e\n    options.UseNpgsql(connectionString));\n```\n    \n\n## Running the solution\n\nBefore the solution can be executed, be sure to run entity framework migrations.\n\n\n### Migration Issues with DbContext\n\nInitial migrations may fail, due to ASP.NET Core template come with a pre-generation migration for SQL Server.\n\nWhen trying to run the migration, you might see errors such as:\n\u003e Npgsql.PostgresException (0x80004005): 42704: type \"nvarchar\" does not exist\n\u003e\n\u003e System.NullReferenceException: Object reference not set to an instance of an object.\n\u003e\n\u003e System.InvalidOperationException: No mapping to a relational type can be found for property 'Microsoft.AspNetCore.Identity.IdentityUser.TwoFactorEnabled' with the CLR type 'bool'.\n\nDelete the entire Migrations folder, and regenerate new inital migrations.\n\nGenerate a new migration using Visual Studio Package Manager Console (from menu: Tools -\u003e NuGet Package Manager -\u003e Package Manager Console):\n\n    PM\u003e Add-Migration\n\nOr, from the command line via DotNet CLI:\n\n    $ dotnet ef migrations add Initial\n\nIf dotnet migration tools don't exist, remember to install the tools using the instruction above in the [environment setup](#environment-setup).\n\n\n### Run Entity Framework Migrations\n\nExecute the migration using either Visual Studio Package Manager Console (from menu: Tools -\u003e NuGet Package Manager -\u003e Package Manager Console):\n\n    PM\u003e Update-Database\n\nOr, from the command line via DotNet CLI, execute the following command inside the project directory, **where the .csproj file is located**:\n\n    $ dotnet ef database update\n\nAfter running the migration, the database is created and web application is ready to be run.\n\n\n## Setting up a PostgresSQL server on Mac\n\nHere are instructions to setup a PostgreSQL server on Mac using Homebrew.\n\n\n### Installing PostgreSQL on Mac\n\nUse [brew](https://brew.sh/) to install PostgreSQL, then launch the service:\n\n    $ brew install postgresql\n    $ brew services start postgresql\n\n\n### Create a user\n\nCreate a user using the `createuser` command from a terminal, where `username` is your desired new user name.  Using the `-P` argument, you will be prompted to setup a password.\n\n    $ createuser username -P\n\n\n### Create a database\n\nCreate your database using the `createdb` command from a terminal, where `dbname` is your desired new database name.\n\n    $ createdb dbname\n    \nAt this time, run the solution's Entity Framework migrations (see above for instructions).\n\n\n### Verifying database\n\nLaunch PostgreSQL interactive terminal and connect to the database.\n\n    $ psql dbname\n\n\nFrom the PostgreSQL interface terminal, List tables using the `\\dt` command:\n\n    dbname=# \\dt\n                       List of relations\n     Schema |         Name          | Type  |    Owner     \n    --------+-----------------------+-------+--------------\n     public | AspNetRoleClaims      | table | username\n     public | AspNetRoles           | table | username\n     public | AspNetUserClaims      | table | username\n     public | AspNetUserLogins      | table | username\n     public | AspNetUserRoles       | table | username\n     public | AspNetUserTokens      | table | username\n     public | AspNetUsers           | table | username\n     public | __EFMigrationsHistory | table | username\n    (8 rows)\n\n\n### Database permissions issues\n\nIf permissions were not setup properly during the creation of the database, retroactively fix by granting privileges where `dbname` is your database name and `username` is the user you created:\n\n    $ psql dbname\n    dbname=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO username;\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonsturges%2Fpostgresql-dotnet-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasonsturges%2Fpostgresql-dotnet-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasonsturges%2Fpostgresql-dotnet-core/lists"}