I’ve been recruited onto a project to modify an existing microservice thats’s written in C# and that runs in a Linux env—CentOS flavor. Presently it persists data as JSON in a key-value store. My task is to reimplement the persistence to use PostgreSQL and an appropriate representation for ordinary SQL manipulation—table(s) with primitive, scalar column datatypes.
So I need a PostgreSQL driver for C#. Preliminary research points to Npgsql as the preferred choice.
It seems that I need to install .NET Core first and that this will provide me with: (1) the env to compile and execute a plain “Hello World” C# terminal app; and (2) the env into which I can install Npgsql. (A terminal app is all I need for now.)
Am I right so far? Or have I gone wrong already?
Googling for “Download .NET Core for Linux” took me HERE. The site says “Ubuntu 19.04 Package Manager – Install .NET Core”. And the install steps use “dpkg” and “apt-get”. These are Debian-only tools. (I understand that Ubuntu is based on Debian.)
Obviously, I can create an Ubuntu VM for my prototype work. (I’ll use Parallels on my MacBook.) But I’d prefer to use CentOS. I’ve read that CentOS, like Ubuntu, is forked from Debian. So, before I risk wasting time:
Has anybody installed .NET Core for Linux on CentOS and then written a C# app that does SQL to a PostgreSQL database?
2
Answers
You are on the right path. .NET Core will run on Linux (CentOS or Ubuntu). Npgsql is a .NET Core-compatible library that will run anywhere where .NET Core runs (Linux, Windows, MacOS). Once you properly install .NET Core, use any IDE to create a sample Hello World application (Visual Studio for Mac or Jetbrains Rider are good options). At that point you can add the latest Npgsql Nuget package and try out the getting started code sample from the docs.
I installed the .Net Core on a CentOS 7 and used Npgsql to connect to my PostgreSql Db, and it works just as good as with any other database and connector.
It seems that I need to install .NET Core first and that this will provide me with: (1) the env to compile and execute a plain “Hello World” C# terminal app; and (2) the env into which I can install Npgsql. (A terminal app is all I need for now.)
That is right so far. I have gone through the same process with a Centos 7 and it works perfectly. To install the .Net core SDK and the .Net core runtime on CentOS you can follow the instructions on the following MSDN link. It is targeting CentOS 7 but the command are sufficiently generic to work directly or with little adaptation effort to a nearby version.
Basically you can run the following commands and you will be done with this part
(To register the product key, repository and install required dependencies)
(To install .Net Core SDK)
(To install Asp.net Core runtime)
(To install .Net core runtime)
At this step you can use a simple HelloWorld-Like terminal app in which you would add a reference to the Npgsql library using (for example)
Your app project will then reflect this change with a package reference tag like:
After this step you can configure a database connection string and use the Npgsql data access classes to connect to your database and perform various operations. You could use for instance the link provided by Shay Rojansky for this purpose.
If your .Net core and your Npgsql package are correctly installed, any other issues you could meet would probably be related to