Compare commits

...

2 Commits

Author SHA1 Message Date
Boris Milašinović
3e38889ada Add JSON function to models 2026-04-22 22:49:47 +02:00
Boris Milašinović
087293f67e Adding execute permission on sh files (Mac and Linux) 2026-04-22 09:45:29 +02:00
10 changed files with 69 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
namespace EF_Demo.Data;
internal static class DbJson
{
public static string? JsonValue(string? expression, string path)
=> throw new NotSupportedException();
}

View File

@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace EF_Demo.Data.MSSQL;
public partial class EventsContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder
.HasDbFunction(typeof(DbJson).GetMethod(nameof(DbJson.JsonValue))!)
.HasName("JSON_VALUE")
.IsBuiltIn();
}
}

View File

@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace EF_Demo.Data.Postgres;
public partial class EventsContext
{
partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
{
modelBuilder
.HasDbFunction(typeof(DbJson).GetMethod(nameof(DbJson.JsonValue))!)
.HasName("jsonb_extract_path_text")
.IsBuiltIn();
}
}

View File

@@ -5,6 +5,7 @@ using EF_Demo.Data.MSSQL;
#endif
using EF_Demo.Models;
using EF_Demo.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
@@ -86,6 +87,12 @@ internal class Demo(IDbContextFactory<EventsContext> contextFactory, ILogger<Dem
/// </summary>
internal void PrintPeople(int year)
{
#if POSTGRES
const string hrPath = "hr";
#else
const string hrPath = "$.hr";
#endif
using var ctx = contextFactory.CreateDbContext();
var query = ctx.People
.Where(p => p.BirthDate.Year == year)
@@ -97,11 +104,12 @@ internal class Demo(IDbContextFactory<EventsContext> contextFactory, ILogger<Dem
p.FirstName, p.LastName,
p.FirstNameTranscription, p.LastNameTranscription,
p.BirthDate,
Country = p.CountryCodeNavigation.Name
Country = p.CountryCodeNavigation.Name,
CountryHr = DbJson.JsonValue(p.CountryCodeNavigation.Translations, hrPath)
});
foreach (var p in query)
{
Console.WriteLine($"{p.FirstName} {p.LastName} ({p.FirstNameTranscription} {p.LastNameTranscription}), {p.Country} {p.BirthDate:dd.MM.yyyy.}");
Console.WriteLine($"{p.FirstName} {p.LastName} ({p.FirstNameTranscription} {p.LastNameTranscription}), {p.Country} / {p.CountryHr} {p.BirthDate:dd.MM.yyyy.}");
}
}
}

View File

@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF_Demo", "EF_Demo.csproj", "{C3669BE8-62DC-69A4-DA79-08849870583B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C3669BE8-62DC-69A4-DA79-08849870583B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3669BE8-62DC-69A4-DA79-08849870583B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3669BE8-62DC-69A4-DA79-08849870583B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3669BE8-62DC-69A4-DA79-08849870583B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ABA15D2D-3AA6-49A6-AA81-E01BF14115A9}
EndGlobalSection
EndGlobal

View File

View File

0
docker-definitions/postgres-eventsdb/init/01-roles.sh Normal file → Executable file
View File

View File

0
docker-definitions/postgres-eventsdb/init/check-db.sh Normal file → Executable file
View File