Traditionally we update the database every time we start our app or publish our app. Without executing the script your database is not the latest and this may cause some issues. Now I can call DbHelper.GetDb() everywhere I need my database and the mapper will be plugged in appropriately.Īs you can see, it is very easy to get up and running with NPoco and PostgreSQL. If your ASP.NET Core project is connecting to your database via entity framework, you cant be unfamiliar with the script: dotnet ef database update. To wire this Mapper up we do the following: var db = new Database( "Conn") Here we check if the Destination Type is “DateTime” or “DateTimeOffset” and convert the Npgsql type into the CLR type. Return (DateTimeOffset?)((NpgsqlTimeStampTZ)x) Return (DateTimeOffset)((NpgsqlTimeStampTZ)x) Return (DateTime?)((NpgsqlTimeStampTZ)x)
|| DestType = typeof(DateTime) || DestType = typeof(DateTime?)) if (DestType = typeof(DateTimeOffset) || DestType = typeof(DateTimeOffset?) Note: Npgsql does not automatically convert “timestamptz” types to DateTimeOffset, but using NPoco’s built in Mapper we can do this ourselves. This will return “TimeStampTz” types using Npgsql’s own type “NpgsqlTimeStampTZ” instead of a plain DateTime. The important part here is that you use the “User Extended Types=true”.
Now we are setup to create our connection string. Once this has been installed, we need to make a change to the web.config. This is then used to simply call stored procedures within NPoco/PetaPoco and avoid magic strings and parameter type guessing. StoredProcedures.tt file automatically generates a C code file with calsses and methods corresponding to your database stored procedure definitions. From the Package Manager Console type: Install-Package Npgsql NPoco/PetaPoco stored procedures with named strong type parameters.
#Npoco idatabase updateasync install#
Firstly you need to install the Npgsql driver from NuGet. Setting up PostgreSQL to run with NPoco is very simple.