@@ -44,6 +44,8 @@ public static PropertyBuilder UseHiLo(
44
44
property . SetValueGenerationStrategy ( NpgsqlValueGenerationStrategy . SequenceHiLo ) ;
45
45
property . SetHiLoSequenceName ( name ) ;
46
46
property . SetHiLoSequenceSchema ( schema ) ;
47
+ property . SetSequenceName ( null ) ;
48
+ property . SetSequenceSchema ( null ) ;
47
49
property . RemoveIdentityOptions ( ) ;
48
50
49
51
return propertyBuilder ;
@@ -115,6 +117,106 @@ public static bool CanSetHiLoSequence(
115
117
116
118
#endregion HiLo
117
119
120
+ #region Sequence
121
+
122
+ /// <summary>
123
+ /// Configures the key property to use a sequence-based key value generation pattern to generate values for new entities,
124
+ /// when targeting PostgreSQL. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
125
+ /// </summary>
126
+ /// <param name="propertyBuilder">The builder for the property being configured.</param>
127
+ /// <param name="name">The name of the sequence.</param>
128
+ /// <param name="schema">The schema of the sequence.</param>
129
+ /// <returns>The same builder instance so that multiple calls can be chained.</returns>
130
+ public static PropertyBuilder UseSequence (
131
+ this PropertyBuilder propertyBuilder ,
132
+ string ? name = null ,
133
+ string ? schema = null )
134
+ {
135
+ Check . NullButNotEmpty ( name , nameof ( name ) ) ;
136
+ Check . NullButNotEmpty ( schema , nameof ( schema ) ) ;
137
+
138
+ var property = propertyBuilder . Metadata ;
139
+
140
+ property . SetValueGenerationStrategy ( NpgsqlValueGenerationStrategy . Sequence ) ;
141
+ property . SetSequenceName ( name ) ;
142
+ property . SetSequenceSchema ( schema ) ;
143
+ property . SetHiLoSequenceName ( null ) ;
144
+ property . SetHiLoSequenceSchema ( null ) ;
145
+
146
+ return propertyBuilder ;
147
+ }
148
+
149
+ /// <summary>
150
+ /// Configures the key property to use a sequence-based key value generation pattern to generate values for new entities,
151
+ /// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
152
+ /// </summary>
153
+ /// <remarks>
154
+ /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
155
+ /// <see href="https://aka.ms/efcore-docs-Npgsql">Accessing SQL Server and SQL Azure databases with EF Core</see>
156
+ /// for more information and examples.
157
+ /// </remarks>
158
+ /// <typeparam name="TProperty">The type of the property being configured.</typeparam>
159
+ /// <param name="propertyBuilder">The builder for the property being configured.</param>
160
+ /// <param name="name">The name of the sequence.</param>
161
+ /// <param name="schema">The schema of the sequence.</param>
162
+ /// <returns>The same builder instance so that multiple calls can be chained.</returns>
163
+ public static PropertyBuilder < TProperty > UseSequence < TProperty > (
164
+ this PropertyBuilder < TProperty > propertyBuilder ,
165
+ string ? name = null ,
166
+ string ? schema = null )
167
+ => ( PropertyBuilder < TProperty > ) UseSequence ( ( PropertyBuilder ) propertyBuilder , name , schema ) ;
168
+
169
+ /// <summary>
170
+ /// Configures the database sequence used for the key value generation pattern to generate values for the key property,
171
+ /// when targeting PostgreSQL.
172
+ /// </summary>
173
+ /// <param name="propertyBuilder">The builder for the property being configured.</param>
174
+ /// <param name="name">The name of the sequence.</param>
175
+ /// <param name="schema">The schema of the sequence.</param>
176
+ /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
177
+ /// <returns>A builder to further configure the sequence.</returns>
178
+ public static IConventionSequenceBuilder ? HasSequence (
179
+ this IConventionPropertyBuilder propertyBuilder ,
180
+ string ? name ,
181
+ string ? schema ,
182
+ bool fromDataAnnotation = false )
183
+ {
184
+ if ( ! propertyBuilder . CanSetSequence ( name , schema , fromDataAnnotation ) )
185
+ {
186
+ return null ;
187
+ }
188
+
189
+ propertyBuilder . Metadata . SetSequenceName ( name , fromDataAnnotation ) ;
190
+ propertyBuilder . Metadata . SetSequenceSchema ( schema , fromDataAnnotation ) ;
191
+
192
+ return name == null
193
+ ? null
194
+ : propertyBuilder . Metadata . DeclaringEntityType . Model . Builder . HasSequence ( name , schema , fromDataAnnotation ) ;
195
+ }
196
+
197
+ /// <summary>
198
+ /// Returns a value indicating whether the given name and schema can be set for the key value generation sequence.
199
+ /// </summary>
200
+ /// <param name="propertyBuilder">The builder for the property being configured.</param>
201
+ /// <param name="name">The name of the sequence.</param>
202
+ /// <param name="schema">The schema of the sequence.</param>
203
+ /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
204
+ /// <returns><see langword="true" /> if the given name and schema can be set for the key value generation sequence.</returns>
205
+ public static bool CanSetSequence (
206
+ this IConventionPropertyBuilder propertyBuilder ,
207
+ string ? name ,
208
+ string ? schema ,
209
+ bool fromDataAnnotation = false )
210
+ {
211
+ Check . NullButNotEmpty ( name , nameof ( name ) ) ;
212
+ Check . NullButNotEmpty ( schema , nameof ( schema ) ) ;
213
+
214
+ return propertyBuilder . CanSetAnnotation ( NpgsqlAnnotationNames . SequenceName , name , fromDataAnnotation )
215
+ && propertyBuilder . CanSetAnnotation ( NpgsqlAnnotationNames . SequenceSchema , schema , fromDataAnnotation ) ;
216
+ }
217
+
218
+ #endregion Sequence
219
+
118
220
#region Serial
119
221
120
222
/// <summary>
@@ -133,8 +235,8 @@ public static PropertyBuilder UseSerialColumn(
133
235
134
236
var property = propertyBuilder . Metadata ;
135
237
property . SetValueGenerationStrategy ( NpgsqlValueGenerationStrategy . SerialColumn ) ;
136
- property . SetHiLoSequenceName ( null ) ;
137
- property . SetHiLoSequenceSchema ( null ) ;
238
+ property . SetSequenceName ( null ) ;
239
+ property . SetSequenceSchema ( null ) ;
138
240
property . RemoveHiLoOptions ( ) ;
139
241
property . RemoveIdentityOptions ( ) ;
140
242
@@ -178,6 +280,8 @@ public static PropertyBuilder UseIdentityAlwaysColumn(this PropertyBuilder prope
178
280
property . SetValueGenerationStrategy ( NpgsqlValueGenerationStrategy . IdentityAlwaysColumn ) ;
179
281
property . SetHiLoSequenceName ( null ) ;
180
282
property . SetHiLoSequenceSchema ( null ) ;
283
+ property . SetSequenceName ( null ) ;
284
+ property . SetSequenceSchema ( null ) ;
181
285
182
286
return propertyBuilder ;
183
287
}
@@ -222,6 +326,8 @@ public static PropertyBuilder UseIdentityByDefaultColumn(this PropertyBuilder pr
222
326
property . SetValueGenerationStrategy ( NpgsqlValueGenerationStrategy . IdentityByDefaultColumn ) ;
223
327
property . SetHiLoSequenceName ( null ) ;
224
328
property . SetHiLoSequenceSchema ( null ) ;
329
+ property . SetSequenceName ( null ) ;
330
+ property . SetSequenceSchema ( null ) ;
225
331
226
332
return propertyBuilder ;
227
333
}
@@ -303,11 +409,17 @@ public static PropertyBuilder<TProperty> UseIdentityColumn<TProperty>(
303
409
NpgsqlAnnotationNames . ValueGenerationStrategy , valueGenerationStrategy , fromDataAnnotation ) )
304
410
{
305
411
propertyBuilder . Metadata . SetValueGenerationStrategy ( valueGenerationStrategy , fromDataAnnotation ) ;
412
+
306
413
if ( valueGenerationStrategy != NpgsqlValueGenerationStrategy . SequenceHiLo )
307
414
{
308
415
propertyBuilder . HasHiLoSequence ( null , null , fromDataAnnotation ) ;
309
416
}
310
417
418
+ if ( valueGenerationStrategy != NpgsqlValueGenerationStrategy . Sequence )
419
+ {
420
+ propertyBuilder . HasSequence ( null , null , fromDataAnnotation ) ;
421
+ }
422
+
311
423
return propertyBuilder ;
312
424
}
313
425
0 commit comments