@@ -11,9 +11,6 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
11
11
/// </summary>
12
12
public class NpgsqlEnumTypeMapping : RelationalTypeMapping
13
13
{
14
- private readonly ISqlGenerationHelper _sqlGenerationHelper ;
15
- private readonly INpgsqlNameTranslator _nameTranslator ;
16
-
17
14
/// <summary>
18
15
/// Translates the CLR member value to the PostgreSQL value label.
19
16
/// </summary>
@@ -25,14 +22,25 @@ public class NpgsqlEnumTypeMapping : RelationalTypeMapping
25
22
/// any release. You should only use it directly in your code with extreme caution and knowing that
26
23
/// doing so can result in application failures when updating to a new Entity Framework Core release.
27
24
/// </summary>
28
- public NpgsqlEnumTypeMapping (
29
- string storeType ,
30
- string ? storeTypeSchema ,
31
- Type enumType ,
32
- ISqlGenerationHelper sqlGenerationHelper ,
33
- INpgsqlNameTranslator ? nameTranslator = null )
25
+ public static NpgsqlEnumTypeMapping Default { get ; } = new ( ) ;
26
+
27
+ /// <summary>
28
+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
29
+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
30
+ /// any release. You should only use it directly in your code with extreme caution and knowing that
31
+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
32
+ /// </summary>
33
+ public INpgsqlNameTranslator NameTranslator { get ; }
34
+
35
+ /// <summary>
36
+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
37
+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
38
+ /// any release. You should only use it directly in your code with extreme caution and knowing that
39
+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
40
+ /// </summary>
41
+ public NpgsqlEnumTypeMapping ( string storeType , Type enumType , INpgsqlNameTranslator ? nameTranslator = null )
34
42
: base (
35
- sqlGenerationHelper . DelimitIdentifier ( storeType , storeTypeSchema ) ,
43
+ storeType ,
36
44
enumType ,
37
45
jsonValueReaderWriter : ( JsonValueReaderWriter ? ) Activator . CreateInstance (
38
46
typeof ( JsonPgEnumReaderWriter < > ) . MakeGenericType ( enumType ) ) )
@@ -46,8 +54,7 @@ public NpgsqlEnumTypeMapping(
46
54
nameTranslator ??= NpgsqlConnection . GlobalTypeMapper . DefaultNameTranslator ;
47
55
#pragma warning restore CS0618
48
56
49
- _nameTranslator = nameTranslator ;
50
- _sqlGenerationHelper = sqlGenerationHelper ;
57
+ NameTranslator = nameTranslator ;
51
58
_members = CreateValueMapping ( enumType , nameTranslator ) ;
52
59
}
53
60
@@ -59,23 +66,32 @@ public NpgsqlEnumTypeMapping(
59
66
/// </summary>
60
67
protected NpgsqlEnumTypeMapping (
61
68
RelationalTypeMappingParameters parameters ,
62
- ISqlGenerationHelper sqlGenerationHelper ,
63
69
INpgsqlNameTranslator nameTranslator )
64
70
: base ( parameters )
65
71
{
66
- _nameTranslator = nameTranslator ;
67
- _sqlGenerationHelper = sqlGenerationHelper ;
72
+ NameTranslator = nameTranslator ;
68
73
_members = CreateValueMapping ( parameters . CoreParameters . ClrType , nameTranslator ) ;
69
74
}
70
75
76
+ // This constructor exists only to support the static Default property above, which is necessary to allow code generation for compiled
77
+ // models. The constructor creates a completely blank type mapping, which will get cloned with all the correct details.
78
+ private NpgsqlEnumTypeMapping ( )
79
+ : base ( "some_enum" , typeof ( int ) )
80
+ {
81
+ #pragma warning disable CS0618 // NpgsqlConnection.GlobalTypeMapper is obsolete
82
+ NameTranslator = NpgsqlConnection . GlobalTypeMapper . DefaultNameTranslator ;
83
+ #pragma warning restore CS0618
84
+ _members = null ! ;
85
+ }
86
+
71
87
/// <summary>
72
88
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
73
89
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
74
90
/// any release. You should only use it directly in your code with extreme caution and knowing that
75
91
/// doing so can result in application failures when updating to a new Entity Framework Core release.
76
92
/// </summary>
77
93
protected override RelationalTypeMapping Clone ( RelationalTypeMappingParameters parameters )
78
- => new NpgsqlEnumTypeMapping ( parameters , _sqlGenerationHelper , _nameTranslator ) ;
94
+ => new NpgsqlEnumTypeMapping ( parameters , NameTranslator ) ;
79
95
80
96
/// <summary>
81
97
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -98,12 +114,31 @@ private static Dictionary<object, string> CreateValueMapping(Type enumType, INpg
98
114
x => x . GetValue ( null ) ! ,
99
115
x => x . GetCustomAttribute < PgNameAttribute > ( ) ? . PgName ?? nameTranslator . TranslateMemberName ( x . Name ) ) ;
100
116
101
- private sealed class JsonPgEnumReaderWriter < T > : JsonValueReaderWriter < T >
117
+ // This is public for the compiled model
118
+ /// <summary>
119
+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
120
+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
121
+ /// any release. You should only use it directly in your code with extreme caution and knowing that
122
+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
123
+ /// </summary>
124
+ public sealed class JsonPgEnumReaderWriter < T > : JsonValueReaderWriter < T >
102
125
where T : struct , Enum
103
126
{
127
+ /// <summary>
128
+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
129
+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
130
+ /// any release. You should only use it directly in your code with extreme caution and knowing that
131
+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
132
+ /// </summary>
104
133
public override T FromJsonTyped ( ref Utf8JsonReaderManager manager , object ? existingObject = null )
105
134
=> Enum . Parse < T > ( manager . CurrentReader . GetString ( ) ! ) ;
106
135
136
+ /// <summary>
137
+ /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
138
+ /// the same compatibility standards as public APIs. It may be changed or removed without notice in
139
+ /// any release. You should only use it directly in your code with extreme caution and knowing that
140
+ /// doing so can result in application failures when updating to a new Entity Framework Core release.
141
+ /// </summary>
107
142
public override void ToJsonTyped ( Utf8JsonWriter writer , T value )
108
143
=> writer . WriteStringValue ( value . ToString ( ) ) ;
109
144
}
0 commit comments