Home / Market / migrating oracle to postgres data access code

migrating oracle to postgres data access code

Migrates .NET/C# data access code from Oracle to PostgreSQL (Npgsql). Replaces Oracle NuGet packages, rewrites OracleConnection/OracleCommand/OracleDataReader usage, fixes DbType mappings, updates stored procedure invocation patterns, and adapts connection string configuration. Use when migrating the application code layer of a .NET project during an Oracle-to-PostgreSQL database migration.

skill★ 417940 dlgithubMITInstall in CosmosUpstream Source

Documentation & Preview

--- name: migrating-oracle-to-postgres-data-access-code description: 'Migrates .NET/C# data access code from Oracle to PostgreSQL (Npgsql). Replaces Oracle NuGet packages, rewrites OracleConnection/OracleCommand/OracleDataReader usage, fixes DbType mappings, updates stored procedure invocation patterns, and adapts connection string configuration. Use when migrating the application code layer of a .NET project during an Oracle-to-PostgreSQL database migration.'

---

Migrating .NET Data Access Code from Oracle to PostgreSQL

Migrate the C# data access layer of a single .Postgres-copy project from Oracle (Oracle.ManagedDataAccess) to PostgreSQL (Npgsql). Work item by item through Reports/{ProjectName}/MigrationChecklist.md.

Prerequisites

Workflow

Progress:
- [ ] Step 1: Replace NuGet packages
- [ ] Step 2: Update connection string configuration
- [ ] Step 3: Rewrite ADO.NET type references
- [ ] Step 4: Fix DbType mappings
- [ ] Step 5: Migrate stored procedure invocation
- [ ] Step 6: Address Oracle-specific SQL and syntax
- [ ] Step 7: Build and verify

Step 1: Replace NuGet packages

In the .csproj of the .Postgres project:

Step 2: Update connection string configuration

Step 3: Rewrite ADO.NET type references

Replace Oracle-specific ADO.NET types with Npgsql equivalents:

| Oracle type | Npgsql replacement | |---|---| | OracleConnection | NpgsqlConnection | | OracleCommand | NpgsqlCommand | | OracleDataReader | NpgsqlDataReader | | OracleDataAdapter | NpgsqlDataAdapter | | OracleParameter | NpgsqlParameter | | OracleTransaction | NpgsqlTransaction | | OracleException | NpgsqlException | | OracleDbType | NpgsqlDbType (from NpgsqlTypes namespace) |

Update using directives accordingly (using Oracle.ManagedDataAccess.Client → using Npgsql).

If the codebase uses IDbConnection/IDbCommand abstractions registered via DI, update only the DI registration and connection string — the consuming code may not need changes.

Step 4: Fix DbType and NpgsqlDbType mappings

Oracle parameter types do not map 1:1 to Npgsql. Review every OracleParameter (now NpgsqlParameter) that sets an explicit type:

| Oracle type | Notes | |---|---| | OracleDbType.Varchar2 | Use NpgsqlDbType.Varchar or omit (Npgsql infers from value) | | OracleDbType.Clob | Use NpgsqlDbType.Text | | OracleDbType.Number | Use NpgsqlDbType.Numeric or NpgsqlDbType.Integer depending on precision | | OracleDbType.Date | Use NpgsqlDbType.Date (date only) or NpgsqlDbType.Timestamp (if time component used) | | OracleDbType.TimeStamp | Use NpgsqlDbType.Timestamp | | OracleDbType.RefCursor | Use NpgsqlDbType.Refcursor — see Step 5 | | OracleDbType.Char | Use NpgsqlDbType.Char |

For parameters where Oracle inferred the type from the value, Npgsql also infers — explicit type setting is often unnecessary and can be removed.

Step 5: Migrate stored procedure invocation

Oracle and PostgreSQL stored procedure invocation differ significantly:

Step 6: Address Oracle-specific SQL and C# patterns

Review inline SQL strings and query builders for Oracle-specific constructs and replace:

| Oracle construct | PostgreSQL replacement | |---|---| | ROWNUM <= n | LIMIT n | | ROWNUM = 1 | LIMIT 1 | | NVL(x, y) | COALESCE(x, y) | | DECODE(expr, v1, r1, ...) | CASE WHEN expr = v1 THEN r1 ... END | | SYSDATE / SYSTIMESTAMP | NOW() or CURRENT_TIMESTAMP | | TO_CHAR(date, fmt) | TO_CHAR(date, fmt) (mostly compatible; verify format strings) | | TO_DATE(str, fmt) | TO_DATE(str, fmt) (verify format strings) | | TO_NUMBER(str) | CAST(str AS NUMERIC) or str::NUMERIC | | || string concat | || (compatible) | | DUAL table | Remove FROM DUAL; PostgreSQL evaluates SELECT expr without a table | | CONNECT BY hierarchy | Rewrite using recursive CTEs (WITH RECURSIVE) | | MERGE INTO | Rewrite as INSERT ... ON CONFLICT DO UPDATE | | Empty string '' as NULL | Oracle treats '' as NULL; PostgreSQL does not — check comparisons and IS NULL guards | | VARCHAR2 | VARCHAR or TEXT |

Step 7: Build and verify

After addressing all checklist items:

  1. Run dotnet build on the .Postgres project. Fix any remaining compilation errors.
  2. Verify no Oracle-specific namespaces remain: search for Oracle.ManagedDataAccess, OracleConnection, OracleCommand, :param patterns.
  3. Mark completed items in Reports/{ProjectName}/MigrationChecklist.md.

EF Core projects

If the project uses Oracle.EntityFrameworkCore:

Key Constraints