Skip to content

Commit Q Dev#1

Open
rducic wants to merge 3 commits intoATX1110from
ATX1110-FIX
Open

Commit Q Dev#1
rducic wants to merge 3 commits intoATX1110from
ATX1110-FIX

Conversation

@rducic
Copy link
Copy Markdown
Owner

@rducic rducic commented Nov 10, 2025

ATX1110 PostgreSQL Migration Fixes

Summary

Fixed multiple compatibility issues when migrating Bob's Bookstore application from SQL Server to PostgreSQL.

Changes Made

1. ReferenceDataItem.cs - Missing Column Mapping

File: app/Bookstore.Domain/ReferenceData/ReferenceDataItem.cs
Issue: PostgresException: 42703: column r.DataType does not exist
Fix: Added missing [Column("datatype")] attribute to map DataType property to database column.

2. ApplicationDbContext.cs - Boolean Type Conversion

File: app/Bookstore.Data/ApplicationDbContext.cs
Issue: PostgresException: 42804: column "wanttobuy" is of type numeric but expression is of type boolean
Fix: Added value conversion for ShoppingCartItem.WantToBuy property to convert boolean to integer.

3. BookRepository.cs - Better Error Handling

File: app/Bookstore.Data/Repositories/BookRepository.cs
Issue: ArgumentNullException: Value cannot be null
Fix: Changed SingleAsync to FirstOrDefaultAsync with null check for better error messages.

4. AuthorsController.cs - SQL Server to EF Core Migration

File: app/Bookstore.Web/Controllers/AuthorsController.cs
Issues:

  • SQL Server stored procedures don't exist in PostgreSQL
  • SqlParameter incompatible with PostgreSQL (requires NpgsqlParameter)
  • SQL Server syntax (DECLARE, EXEC, [dbo], FORMAT, DATEDIFF) not supported
  • Methods returning null causing view errors

Fixes:

  • Replaced stored procedure calls with EF Core operations
  • Converted raw SQL queries to LINQ
  • Return empty lists instead of null
  • Fixed nullable DateTime handling

5. ProductsController.cs - Stored Procedure Removal

File: app/Bookstore.Web/Controllers/ProductsController.cs
Issue: PostgresException: 42601: syntax error at or near "["
Fix: Replaced SQL Server stored procedure [dbo].[uspGetProductData]() with EF Core query _context.Product.ToListAsync().

Root Cause

The application was originally designed for SQL Server but was migrated to PostgreSQL without updating:

  • Database-specific SQL syntax
  • Stored procedure dependencies
  • Parameter types
  • Column mappings
  • Data type conversions

Solution Approach

Converted all database operations to use Entity Framework Core, which provides:

  • Database-agnostic operations
  • Automatic parameter handling
  • Cross-platform compatibility
  • Simplified maintenance

Files Modified

  1. app/Bookstore.Domain/ReferenceData/ReferenceDataItem.cs
  2. app/Bookstore.Data/ApplicationDbContext.cs
  3. app/Bookstore.Data/Repositories/BookRepository.cs
  4. app/Bookstore.Web/Controllers/AuthorsController.cs
  5. app/Bookstore.Web/Controllers/ProductsController.cs

All changes maintain existing functionality while ensuring PostgreSQL compatibility.�

@rducic
Copy link
Copy Markdown
Owner Author

rducic commented Nov 10, 2025

For list of full chenages and reason why they were necessary please see https://github.com/rducic/AtxBobsBookstore/blob/ATX1110-FIX/ATX1110-FIX-changes.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant