Integrating PostgreSQL with Entity Framework Core (EF Core) is a key step in building efficient and
maintainable .NET applications. EF Core offers a modern, high-performance ORM for streamlined data access, while PostgreSQL brings reliability and advanced database features.
In this step-by-step guide, you'll learn how to connect your C# application to PostgreSQL using dotConnect for PostgreSQL, generate EF Core models, and implement database operations with ease.
Why dotConnect for PostgreSQL?
Advanced ORM support
Fully supports EF Core, Dapper, NHibernate, LinqConnect, and other modern data access technologies for efficient and reliable data management.
Full ADO.NET compliance
Conforms to the latest ADO.NET standards and recent industry innovations for seamless and consistent integration with .NET applications.
PostgreSQL-specific data types
Includes many PostgreSQL-specific features and fully supports all unique data types for accurate and complete data representation.
Secure connection options
Provides robust security with support for SSL/SSH connections, connecting via proxy servers, embedded servers, and HTTP tunneling.
IDE integration
Features native integration with Visual Studio and complete design-time support for accelerated development.
Priority support & frequent updates
Includes priority support, detailed documentation, and regular updates for continuous improvement.
Download and activate dotConnect for PostgreSQL
You can start using dotConnect for PostgreSQL immediately with a 30-day free trial. Choose one of the following installation options:
Once you have configured the database, you can proceed to the next step: creating an EF Core model. You can do this in two ways: using Scaffold-DbContext or via Entity Developer.
Create an EF Core model via Entity Developer
Entity Developer allows you to visually design and generate EF Core models, making database application development faster, easier, and more efficient. If you don't have it already installed, close your Visual Studio instance, download Entity Developer, and install it following the on-screen instructions.
Follow the detailed illustrated guide to create your database model using Entity Developer.
When this process is complete, the model you created opens, allowing you to work with it.
This guideline was designed with Visual Studio in mind. However, if you prefer VS Code, JetBrains Rider, or another similar tool, you can use ED as a
standalone application to work with the models efficiently.
Create an EF Core model using Scaffold-DbContext
You can use Scaffold-DbContext to generate DbContext and entity classes for your database. Run the following command, replacing values with your actual credentials:
After you execute this command, the ModelContext file and the Models folder containing the table entity classes get generated.
Connection strings
Property
Description
Host or Server
Host name or IP address of the PostgreSQL server
Port
Port on which the PostgreSQL server listens
UserId
User ID used to authenticate with PostgreSQL
Password
Password for the user ID
Database
Default database to use after connecting
Schema
PostgreSQL schema to use
LicenseKey
Your license key. This is required only when you use .NET Standard-compatible assemblies.
Connect to PostgreSQL and retrieve data
This section connects to a PostgreSQL database and retrieves the first 10 rows from the Actor table. The dvdrentalModel DbContext is configured with a
connection string in the OnConfiguring method.
Run the application to execute the query. The retrieved data — the first 10 records from the Actor table — will be printed in the console output.
Insert new records using EF Core
This section inserts a new test actor into the Actor table using EF Core’s Add and SaveChanges methods. After executing the operation,
the new record is persisted to the database, and you can verify the insertion by querying the table or viewing the console output.
classProgram
{
staticvoidMain(string[] args)
{
try
{
usingvar context = new dvdrentalModel();
var newActor = new Actor
{
FirstName = "Test",
LastName = "Actor",
LastUpdate = DateTime.Now
};
context.Actors.Add(newActor);
int rows = context.SaveChanges();
Console.WriteLine($"Inserted Actor - ID: {newActor.ActorId}, Name: {newActor.FirstName}{newActor.LastName}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Console.ReadKey();
}
}
The console shows the inserted record, confirming that the data was successfully saved to the database. You should see the newly added actor's ID along with their first and last name.
Update PostgreSQL data using EF Core
This section updates the test actor’s first and last names. The record is retrieved, modified, and saved with SaveChanges. Once the update is complete, the console displays the updated values, verifying that the changes were successfully applied to the database.
The console displays the updated record, showing the modified first and last names. This confirms that the changes have been persisted correctly in the database.
Delete PostgreSQL data using EF Core
This section deletes the test actor from the Actor table using Remove and SaveChanges. After the deletion is committed, the actor record is permanently
removed from the database.
classProgram
{
staticvoidMain(string[] args)
{
try
{
usingvar context = new dvdrentalModel();
var actor = context.Actors.FirstOrDefault(a => a.FirstName == "Updated" && a.LastName == "Star");
if (actor != null)
{
context.Actors.Remove(actor);
context.SaveChanges();
Console.WriteLine($"Deleted Actor - ID: {actor.ActorId}, Name: {actor.FirstName}{actor.LastName}");
}
else
{
Console.WriteLine("Actor not found.");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Console.ReadKey();
}
}
The console confirms the deletion by indicating that the record has been successfully removed.
Video tutorial: Connect to PostgreSQL with EF Core
Conclusion
Integrating Entity Framework Core with PostgreSQL provides a solid foundation for building scalable and efficient .NET applications. By leveraging the capabilities of
EF Core and dotConnect for PostgreSQL, developers can streamline data access, reduce boilerplate code, and maintain cleaner architecture. This guide equips you with the tools
and techniques needed to develop high-performance applications backed by the reliability of PostgreSQL.
FAQ
How do you install and activate dotConnect for PostgreSQL in a .NET project?
Install dotConnect for PostgreSQL either by running the Windows installer (EXE) or by adding the Devart.Data.PostgreSql NuGet package to your .NET project. Then, retrieve your activation key from the Devart Customer Portal and specify it in your connection string by using the License Key parameter to activate the provider and connect successfully.
How do you create a connection to PostgreSQL using dotConnect in C#?
Define a connection string that includes Host, User Id, Password, Database, and (if required) License Key. Then create a PgSqlConnection with this string and call Open() inside a try/catch block to verify the connection and handle any errors.
How do you enable SSL/TLS for secure PostgreSQL connections with dotConnect?
Add SslMode=Require and provide the certificate file paths (for example, CACert, Cert, and Key) in the connection string. Then create a PgSqlConnection with this string and call Open() to establish an encrypted SSL/TLS session.
Can you connect to PostgreSQL using Entity Framework Core and dotConnect?
Yes, you can connect to PostgreSQL using Entity Framework Core and dotConnect. You can either use Entity Developer to visually create an EF Core model from the database and generate the DbContext and entity classes, or run Scaffold-DbContext with a dotConnect connection string (including the License Key) to scaffold the DbContext and entities from an existing database.
Is it possible to connect to PostgreSQL using Visual Studio Server Explorer with dotConnect?
Yes. In Visual Studio Server Explorer, you can add a new Data Connection, select dotConnect for PostgreSQL as the data provider, enter your PostgreSQL connection details, test the connection, and then browse and work with database objects directly in the IDE.
I'm a technical content writer who loves turning complex topics — think SQL, connectors, and backend chaos–into content that actually makes sense (and maybe even makes you smile). I write for devs, data folks, and curious minds who want less fluff and more clarity. When I'm not wrangling words, you'll find me dancing salsa, or hopping between cities.