22using System . Collections . Generic ;
33using Microsoft . EntityFrameworkCore ;
44using System . Configuration ;
5- using System . Data . SqlClient ;
5+ using Npgsql ;
66using Microsoft . Extensions . Configuration ;
77using Microsoft . AspNetCore . DataProtection . EntityFrameworkCore ;
88using System . IO ;
@@ -26,8 +26,33 @@ public EmailServiceContext(DbContextOptions<EmailServiceContext> options)
2626
2727 protected override void OnModelCreating ( ModelBuilder modelBuilder )
2828 {
29+ modelBuilder . HasDefaultSchema ( "dbo" ) ;
30+
2931 modelBuilder . Entity < EnqueueIncomingMessage > ( ) . HasNoKey ( ) ;
30- modelBuilder . Entity < ApiKey > ( ) . HasKey ( e => e . Id ) ;
32+
33+ // Explicitly map ApiKey columns to match PostgreSQL naming
34+ modelBuilder . Entity < ApiKey > ( entity =>
35+ {
36+ entity . ToTable ( "apikeys" ) ;
37+ entity . HasKey ( e => e . Id ) ;
38+ entity . Property ( e => e . Id ) . HasColumnName ( "id" ) ;
39+ entity . Property ( e => e . Name ) . HasColumnName ( "name" ) ;
40+ entity . Property ( e => e . KeyHash ) . HasColumnName ( "keyhash" ) ;
41+ entity . Property ( e => e . KeyPrefix ) . HasColumnName ( "keyprefix" ) ;
42+ entity . Property ( e => e . IsActive ) . HasColumnName ( "isactive" ) ;
43+ entity . Property ( e => e . CreatedDate ) . HasColumnName ( "createddate" ) ;
44+ entity . Property ( e => e . LastUsedDate ) . HasColumnName ( "lastuseddate" ) ;
45+ entity . Property ( e => e . Description ) . HasColumnName ( "description" ) ;
46+ } ) ;
47+
48+ // Explicitly map DataProtectionKey columns to match PostgreSQL naming
49+ modelBuilder . Entity < DataProtectionKey > ( entity =>
50+ {
51+ entity . ToTable ( "dataprotectionkeys" ) ;
52+ entity . Property ( e => e . Id ) . HasColumnName ( "id" ) ;
53+ entity . Property ( e => e . FriendlyName ) . HasColumnName ( "friendlyname" ) ;
54+ entity . Property ( e => e . Xml ) . HasColumnName ( "xml" ) ;
55+ } ) ;
3156
3257 OnModelCreatingPartial ( modelBuilder ) ;
3358 }
@@ -36,48 +61,31 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3661
3762 public int EnqueueIncomingMessagesRun ( string userName , string title , string ? CreatedEmail , string ? CreatedName , bool ? isSecure , string bodyHtml , string ? messageType , bool ? isImportantTag , string ? ccEmail , string ? bccEmail )
3863 {
39- var parameters = new List < Microsoft . Data . SqlClient . SqlParameter >
40- {
41- new Microsoft . Data . SqlClient . SqlParameter ( "@UserName" , userName ) ,
42- new Microsoft . Data . SqlClient . SqlParameter ( "@Title" , title ) ,
43- new Microsoft . Data . SqlClient . SqlParameter ( "@BodyHtml" , bodyHtml ) ,
44- } ;
45-
46- AddNullableParameter ( parameters , "@CreatedEmail" , CreatedEmail ) ;
47- AddNullableParameter ( parameters , "@CreatedName" , CreatedName ) ;
48- AddNullableParameter ( parameters , "@MessageType" , messageType ) ;
49- AddNullableParameter ( parameters , "@CCEmail" , ccEmail ) ;
50- AddNullableParameter ( parameters , "@BCCEmail" , bccEmail ) ;
51-
52- parameters . Add ( new Microsoft . Data . SqlClient . SqlParameter ( "@IsSecure" , ( object ) isSecure ?? DBNull . Value ) ) ;
53- parameters . Add ( new Microsoft . Data . SqlClient . SqlParameter ( "@IsImportantTag" , ( object ) isImportantTag ?? DBNull . Value ) ) ;
54-
55- var sqlParameters = parameters . ToArray ( ) ;
56-
5764 try
5865 {
59- // Use ExecuteSqlRaw to execute the command without expecting a result set.
60- this . Database . ExecuteSqlRaw ( "EXEC EnqueueIncomingMessages @UserName, @Title, @CreatedEmail, @CreatedName, @IsSecure, @BodyHtml, @MessageType, @IsImportantTag, @CCEmail, @BCCEmail" , sqlParameters ) ;
66+ // Use SELECT to execute the PostgreSQL function with proper parameter binding
67+ var result = this . Database . ExecuteSqlRaw (
68+ "SELECT dbo.enqueueincomingmessages({0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9})" ,
69+ userName ,
70+ title ,
71+ ( object ? ) CreatedEmail ?? DBNull . Value ,
72+ ( object ? ) CreatedName ?? DBNull . Value ,
73+ ( object ? ) isSecure ?? DBNull . Value ,
74+ bodyHtml ,
75+ ( object ? ) messageType ?? DBNull . Value ,
76+ ( object ? ) isImportantTag ?? DBNull . Value ,
77+ ( object ? ) ccEmail ?? DBNull . Value ,
78+ ( object ? ) bccEmail ?? DBNull . Value
79+ ) ;
6180 return 1 ;
6281 }
63- catch ( Exception e )
82+ catch ( Exception ex )
6483 {
6584 // Log exception as needed
85+ Console . WriteLine ( $ "Error executing enqueueincomingmessages: { ex . Message } ") ;
6686 return - 1 ;
6787 }
6888 }
6989
70- private void AddNullableParameter ( List < Microsoft . Data . SqlClient . SqlParameter > parameters , string parameterName , string ? parameterValue )
71- {
72- if ( parameterValue != null )
73- {
74- parameters . Add ( new Microsoft . Data . SqlClient . SqlParameter ( parameterName , parameterValue ) ) ;
75- }
76- else
77- {
78- parameters . Add ( new Microsoft . Data . SqlClient . SqlParameter ( parameterName , DBNull . Value ) ) ;
79- }
80- }
81-
8290 }
8391}
0 commit comments