Skip to content

Latest commit

 

History

History

PostgreSqlEFCore

PostgreSQL Connector Sample App - Entity Framework Core

ASP.NET Core sample app illustrating how to use Entity Framework Core together with the Steeltoe PostgreSQL Connector for connecting to a PostgreSQL database. There is also an additional sample that illustrates how to use a NpgsqlConnection to issue commands to the bound database.

General pre-requisites

  1. Installed .NET 8 SDK
  2. Optional: VMware Tanzu Platform for Cloud Foundry (optionally with Windows support) with VMware Tanzu Cloud Service Broker and Cloud Foundry CLI
  3. Optional: VMware Tanzu Platform for Kubernetes v1.5 or higher and Kubernetes

Running locally

  1. Start a PostgreSQL docker container
  2. Run the sample
    dotnet run
    

Upon startup, the app inserts a couple of rows into the bound PostgreSQL database. They are displayed on the home page.

Running on Tanzu Platform for Cloud Foundry

  1. Create a PostgreSQL service instance in an org/space
    cf target -o your-org -s your-space
    
    • When using the Cloud Service Broker for Azure:
      cf create-service csb-azure-postgresql mini samplePostgreSqlService
      
    • When using the Cloud Service Broker for GCP:
      cf create-service csb-google-postgres gcp-postgres-tiny samplePostgreSqlService
      
  2. Wait for the service to become ready (you can check with cf services)
  3. Run the cf push command to deploy from source (you can monitor logs with cf logs postgresql-efcore-connector-sample)
    • When deploying to Windows, binaries must be built locally before push. Use the following commands instead:
      dotnet publish -r win-x64 --self-contained
      cf push -f manifest-windows.yml -p bin/Release/net8.0/win-x64/publish
      
  4. Copy the value of routes in the output and open in your browser

Running on Tanzu Platform for Kubernetes

Create PostgreSQL class claim

In order to connect to PostgreSQL for this sample, you must have a class claim available for the application to bind to. The commands listed below will create the claim, and the claim will be bound to the application via the definition in the workload.yaml that is included in the config folder of this project.

kubectl config set-context --current --namespace=your-namespace
tanzu service class-claim create my-postgresql-service --class postgresql-unmanaged

If you'd like to learn more about these services, see claiming services and consuming services in the documentation.

App deployment

To deploy from local source code:

tanzu app workload apply --local-path . --file ./config/workload.yaml -y

Alternatively, from locally built binaries:

dotnet publish -r linux-x64 --no-self-contained
tanzu app workload apply --local-path ./bin/Release/net8.0/linux-x64/publish --file ./config/workload.yaml -y

See the Tanzu documentation for details.


See the Official Steeltoe Connectors Documentation for a more in-depth walkthrough of the samples and more detailed information.