Click here to Skip to main content
15,887,927 members
Articles / Programming Languages / C#
Tip/Trick

PostgreSQL-ADO.NET Framework

Rate me:
Please Sign up or sign in to vote.
4.38/5 (4 votes)
15 Mar 2018GPL33 min read 22.6K   742   6   3
The simple sample that shows how to connect the ADO.NET Framework to the PostgreSQL

Introduction

The simple application that shows how to connect the ADO.NET Framework to the PostgreSQL. The source code is based on the N-Tier pattern and uses EntityFramework6.Npgsql and Npgsql NuGet packages.

Background

PostgreSQL, unlike MS SQL Server, is completely free. It also has the ability to connect .NET applications via Entity Framework and some other packages. It also gives a good performance. So, it seems like a good free alternative for a SQL Server in applications.

Using the Code

As I described above, the source code is based on the N-Tier design and has 4 layers:

  1. Repository
  2. Model
  3. Service
  4. GUI

The solution also has a generic project that names XMen.A query that is a generic Query creator. Generic query generators have this ability that makes the independent query. The independent queries do not have any dependency on the type of database. They will be translated into the correct Query in the Repository layer.

In the Model layer, all classes are inherited from a superclass named Entity.

C#
namespace SMS.Model
{
    public class Entity
    {
        public Guid Id { get; set; }
    }
}

Actually, it looks very simple but all the code in the application is based on this pattern. Model, Repository, and Service. It helps to have a hierarchical design in our source code. For example, you can see the structure of the Course class here:

C#
namespace SMS.Model
{
    public class Cours:
        Entity
    {
        public string Name { get; set; }

    }
}

So as you can see, it's inherited from the Entity class. Also, this Model has its own repository and services in both Service and Repository layer.

For connecting the ADO.NET Entity Framework to the PostgreSQL, you should add two NuGet packages plus original ADO.NET Entity Framework. I have to pay attention that you should use Entity Framework 6.0 in your application and Entity Framework 5.0 is not compatible with PostgreSQL. Two NuGet packages are:

  1. Npgsql
  2. EntityFramework6.Npgsql

For installing each of them, you can use Visual Studio NuGet GUI or NuGet Command. But you don't have enough told yet. If you want to create a new entity schema and connect it to the PostgreSQL, you should install a simple Visual Studio extension that adds the PostgreSQL connection setting to your Visual Studio. The extension name is Npgsql PostgreSQL Integration. After you install it, you will have a new option in the Data source list box:

Image 1

It lets you choose that your data source is a PostgreSQL. The connection properties for PostgreSQL are like that:

Image 2

As a default, you can use 127.0.0.1 for the database server that is hosted on your computer. And after you filled all the necessary information, you can see the normal windows of Entity Framework and make your schema.

When you are working with the Entity Framework, you have two options for model and context. First, using the models and context file that the Visual Studio created automatically, second, using your own model and context. If you choose the second way, you should be aware that after each time you save your schema, the Visual Studio creates such files and these are conflicting with your context and model files.

In the photo, you can see samples of the generated files. You should delete them manually each time after you save the schema.

Image 3

Points of Interest

I think that PostgreSQL has a good future. It's free but it has several features especially that can work with .NET solutions. It's really better than Microsoft SQL Server Express and you can replace it very easy. So, I've started to work with the PostgreSQL.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
Sweden Sweden
Mehdi Mohseni is a Software senior developer (Consultant) in Sigma ITC (www.sigmait.se), Mehdi has a deep experience in N-Tier software applications as well as MVC design pattern. Mehdi has led more than 100 Asp.Net C# or VB.Net Automation applications. Mehdi is working in Toyota Material Handling Logistic Solution as Senior .Net Developer now.

Comments and Discussions

 
QuestionNeed to setup code at my machine Pin
Deepak Kumar CSJM16-Apr-19 5:11
Deepak Kumar CSJM16-Apr-19 5:11 
QuestionProblem Pin
Sudhir Dehade15-Mar-18 22:26
Sudhir Dehade15-Mar-18 22:26 
PraiseDownvoters Pin
RickZeeland15-Mar-18 7:00
mveRickZeeland15-Mar-18 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.