Christian!
Npgsql, like other providers, aren't thread safe. As you are using multiple threads with the same connection you end up with this problem.
In order to make it work, you have to put the line which creates the connection inside the Parallel.For loop:
string SqlParallel = "";
int OID = -1;
Parallel.For(0, 100, i =>
{
using (NpgsqlConnection PGconnexion = new NpgsqlConnection(...))
{
SqlParallel = string.Format(@"INSERT INTO ParallelTest(Id) VALUES({0}) RETURNING OID;", i);
using (NpgsqlCommand PGcommandParallel = new NpgsqlCommand(SqlParallel, PGconnexion))
{
try { OID = (int)PGcommandParallel.ExecuteScalar(); }
catch (Exception ex) { Console.WriteLine(ex.Message); }
}
Console.WriteLine("Insert: {0} OID: {1}", i, OID);
}
});
I tested it here and it worked ok. Please, give it a try and let me know if you still have an issue with this change. Note that I used the "using" clause so the connection is closed as soon as the block finishes.
I hope it helps.