Add JSON function to models
This commit is contained in:
7
DataAccess/EF_Demo/Data/DbJson.cs
Normal file
7
DataAccess/EF_Demo/Data/DbJson.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace EF_Demo.Data;
|
||||
|
||||
internal static class DbJson
|
||||
{
|
||||
public static string? JsonValue(string? expression, string path)
|
||||
=> throw new NotSupportedException();
|
||||
}
|
||||
14
DataAccess/EF_Demo/Data/MSSQL/EventsContext.Custom.cs
Normal file
14
DataAccess/EF_Demo/Data/MSSQL/EventsContext.Custom.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
14
DataAccess/EF_Demo/Data/Postgres/EventsContext.Custom.cs
Normal file
14
DataAccess/EF_Demo/Data/Postgres/EventsContext.Custom.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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.}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
24
DataAccess/EF_Demo/EF_Demo.sln
Normal file
24
DataAccess/EF_Demo/EF_Demo.sln
Normal 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
|
||||
Reference in New Issue
Block a user