Tuesday, September 26, 2017

NPGSQL–Relation does not exist

PostgreSQL has great .NET support thanks to the open source NPGSQL library.

From the home page:

Npgsql is an open source ADO.NET Data Provider for PostgreSQL, it allows programs written in C#, Visual Basic, F# to access the PostgreSQL database server. It is implemented in 100% C# code, is free and is open source.

In addition, providers have been written for Entity Framework Core and for Entity Framework 6.x.

However I immediately had some problems the moment I tried to execute a query. Strange thing was that my code almost the same as what could be found on the Getting Started page;

The error I got was the following:

Query failed: ERROR: relation "Northwind.Products" does not exist

I tried to execute the same query directly in the PGAdmin tool and indeed I got the same error.

What am I doing wrong? The problem is that PostgreSQL by default implicitly converts unquoted identifiers in my query to lowercase.

So the following query;

SELECT Id, ProductName FROM Northwind.Products

was transformed to

SELECT id, productname FROM northwind.products

As object names are case sensitive in PostgreSQL(or so it seems), this resulted in the fact that my table was not found.

There are 2 possible solutions:

  • Use quotes around your identifiers: SELECT “Id”, “ProductName” FROM “Northwind”.”Products”
  • Change the casing of your database objects(tables, columns, …) to lowercase

I choose the last option, because I had to escape my query in string in my C# code otherwise.

1 comment:

lakshmi deepa said...

Its a wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much
SAP Training in Chennai
SAP Basis Training in Chennai
SAP SD Training in Chennai
SAP FICO Training in Chennai