Skip to content

Commit 3a4dcad

Browse files
committed
Switch to PostgresSQL
1 parent bda6ee8 commit 3a4dcad

5 files changed

Lines changed: 47 additions & 40 deletions

File tree

EmailService-API/EmailService-API.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.22" />
1414
<PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.22" />
15-
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
1615
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.22" />
1716
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.22" />
18-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.22" />
1917
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
2018
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
19+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.11" />
2120
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2221
</ItemGroup>
2322

EmailService-API/Models/ApiKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace EmailService_API.Models
55
{
6-
[Table("ApiKeys")]
6+
[Table("apikeys")]
77
public class ApiKey
88
{
99
[Key]

EmailService-API/Models/EmailServiceContext.cs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using Microsoft.EntityFrameworkCore;
44
using System.Configuration;
5-
using System.Data.SqlClient;
5+
using Npgsql;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.AspNetCore.DataProtection.EntityFrameworkCore;
88
using 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
}

EmailService-API/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void Main(string[] args)
1818
// Add services to the container.
1919
var connectionString = builder.Configuration.GetConnectionString("EmailServiceContextConnection");
2020
builder.Services.AddDbContext<EmailServiceContext>(options =>
21-
options.UseSqlServer(connectionString));
21+
options.UseNpgsql(connectionString));
2222

2323
// Configure Data Protection to use database
2424
builder.Services.AddDataProtection()

EmailService-API/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
},
88
"AllowedHosts": "*",
99
"ConnectionStrings": {
10-
"EmailServiceContextConnection": "Server=192.168.10.105, 1433; User ID=svc_emailservice;Password=2G_Ifds*0vN;Database=EmailService;Trusted_Connection=False;TrustServerCertificate=True"
10+
"EmailServiceContextConnection": "Host=192.168.10.105;Port=5432;Database=emailservice;Username=svc_emailservice;Password=2G_Ifds*0vN;"
1111
}
1212
}

0 commit comments

Comments
 (0)