11using Issue12PostgreSql . Entities ;
22using Microsoft . EntityFrameworkCore ;
3- using Microsoft . Extensions . Logging ;
43
54namespace Issue12PostgreSql . DataLayer ;
65
76public class ApplicationDbContext : DbContext
87{
9- public ApplicationDbContext ( DbContextOptions options )
10- : base ( options )
8+ public ApplicationDbContext ( DbContextOptions options ) : base ( options )
119 {
1210 }
1311
1412 public DbSet < Person > People { get ; set ; }
13+
1514 public DbSet < Address > Addresses { get ; set ; }
15+
1616 public DbSet < Book > Books { get ; set ; }
17+
1718 public DbSet < Entity > Entities { get ; set ; }
1819
1920 protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
2021 {
21- optionsBuilder
22- . UseLoggerFactory ( LoggerFactory . Create ( x => x
23- . AddConsole ( )
24- . AddFilter ( y => y >= LogLevel . Debug ) ) )
25- . EnableSensitiveDataLogging ( )
26- . EnableDetailedErrors ( ) ;
22+ optionsBuilder . EnableSensitiveDataLogging ( ) . EnableDetailedErrors ( ) ;
2723
2824 base . OnConfiguring ( optionsBuilder ) ;
2925 }
30-
31- protected override void OnModelCreating ( ModelBuilder builder )
32- {
33- base . OnModelCreating ( builder ) ;
34-
35- /*foreach (var property in builder.Model.GetEntityTypes()
36- .SelectMany(t => t.GetProperties())
37- .Where(p => p.GetColumnType() == "jsonb"))
38- {
39- var converterType = typeof(JsonbConvertor<>).MakeGenericType(property.ClrType);
40- var converter = (ValueConverter)Activator.CreateInstance(converterType, (object)null);
41- property.SetValueConverter(converter);
42- }*/
43-
44- // It does not support DateTimeOffset
45- /*foreach (var property in builder.Model.GetEntityTypes()
46- .SelectMany(t => t.GetProperties())
47- .Where(p => p.ClrType == typeof(DateTimeOffset)))
48- {
49- property.SetValueConverter(
50- new ValueConverter<DateTimeOffset, DateTime>(
51- convertToProviderExpression: dateTimeOffset => dateTimeOffset.UtcDateTime,
52- convertFromProviderExpression: dateTime => new DateTimeOffset(dateTime)
53- ));
54- }
55-
56- foreach (var property in builder.Model.GetEntityTypes()
57- .SelectMany(t => t.GetProperties())
58- .Where(p => p.ClrType == typeof(DateTimeOffset?)))
59- {
60- property.SetValueConverter(
61- new ValueConverter<DateTimeOffset?, DateTime>(
62- convertToProviderExpression: dateTimeOffset => dateTimeOffset.Value.UtcDateTime,
63- convertFromProviderExpression: dateTime => new DateTimeOffset(dateTime)
64- ));
65- }*/
66-
67- // Supporting DateOnly
68- /*foreach (var property in builder.Model.GetEntityTypes()
69- .SelectMany(t => t.GetProperties())
70- .Where(p => p.ClrType == typeof(DateOnly)))
71- {
72- property.SetValueConverter(
73- new ValueConverter<DateOnly, DateTime>(
74- convertToProviderExpression: dateOnly => dateOnly.ToDateTime(new TimeOnly(0, 0)),
75- convertFromProviderExpression: dateTime => DateOnly.FromDateTime(dateTime)
76- ));
77- }
78-
79- foreach (var property in builder.Model.GetEntityTypes()
80- .SelectMany(t => t.GetProperties())
81- .Where(p => p.ClrType == typeof(DateOnly?)))
82- {
83- property.SetValueConverter(
84- new ValueConverter<DateOnly?, DateTime>(
85- convertToProviderExpression: dateOnly => dateOnly.Value.ToDateTime(new TimeOnly(0, 0)),
86- convertFromProviderExpression: dateTime => DateOnly.FromDateTime(dateTime)
87- ));
88- }*/
89-
90- // Supporting TimeOnly
91- /*foreach (var property in builder.Model.GetEntityTypes()
92- .SelectMany(t => t.GetProperties())
93- .Where(p => p.ClrType == typeof(TimeOnly)))
94- {
95- property.SetValueConverter(
96- new ValueConverter<TimeOnly, TimeSpan>(
97- convertToProviderExpression: timeOnly => timeOnly.ToTimeSpan(),
98- convertFromProviderExpression: timeSpan => TimeOnly.FromTimeSpan(timeSpan)
99- ));
100- }
101-
102- foreach (var property in builder.Model.GetEntityTypes()
103- .SelectMany(t => t.GetProperties())
104- .Where(p => p.ClrType == typeof(TimeOnly?)))
105- {
106- property.SetValueConverter(
107- new ValueConverter<TimeOnly?, TimeSpan>(
108- convertToProviderExpression: timeOnly => timeOnly.Value.ToTimeSpan(),
109- convertFromProviderExpression: timeSpan => TimeOnly.FromTimeSpan(timeSpan)
110- ));
111- }*/
112- }
113- }
114-
115- /*public class JsonbConvertor<T> : ValueConverter<T, string>
116- {
117- private static readonly Expression<Func<T, string>> _convertToProviderExpression = x => Serialize(x);
118- private static readonly Expression<Func<string, T>> _convertFromProviderExpression = x => Deserialize(x);
119-
120- public JsonbConvertor(ConverterMappingHints mappingHints = null)
121- : base(_convertToProviderExpression, _convertFromProviderExpression, mappingHints)
122- { }
123-
124- private static string Serialize(T x)
125- {
126- return JsonSerializer.Serialize(x);
127- }
128-
129- private static T Deserialize(string x)
130- {
131- return JsonSerializer.Deserialize<T>(x);
132- }
133- }*/
26+ }
0 commit comments