-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
SqlServerPropertyBuilderExtensions.cs
815 lines (753 loc) · 44.1 KB
/
SqlServerPropertyBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore;
/// <summary>
/// SQL Server specific extension methods for <see cref="PropertyBuilder" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
public static class SqlServerPropertyBuilderExtensions
{
/// <summary>
/// Configures the key property to use a sequence-based hi-lo pattern to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder UseHiLo(
this PropertyBuilder propertyBuilder,
string? name = null,
string? schema = null)
{
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
var property = propertyBuilder.Metadata;
name ??= SqlServerModelExtensions.DefaultHiLoSequenceName;
var model = property.DeclaringType.Model;
if (model.FindSequence(name, schema) == null)
{
model.AddSequence(name, schema).IncrementBy = 10;
}
property.SetValueGenerationStrategy(SqlServerValueGenerationStrategy.SequenceHiLo);
property.SetHiLoSequenceName(name);
property.SetHiLoSequenceSchema(schema);
property.SetIdentitySeed(null);
property.SetIdentityIncrement(null);
return propertyBuilder;
}
/// <summary>
/// Configures the key property to use a sequence-based hi-lo pattern to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TProperty> UseHiLo<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
string? name = null,
string? schema = null)
=> (PropertyBuilder<TProperty>)UseHiLo((PropertyBuilder)propertyBuilder, name, schema);
/// <summary>
/// Configures the database sequence used for the hi-lo pattern to generate values for the key property,
/// when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>A builder to further configure the sequence.</returns>
public static IConventionSequenceBuilder? HasHiLoSequence(
this IConventionPropertyBuilder propertyBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
if (!propertyBuilder.CanSetHiLoSequence(name, schema, fromDataAnnotation))
{
return null;
}
propertyBuilder.Metadata.SetHiLoSequenceName(name, fromDataAnnotation);
propertyBuilder.Metadata.SetHiLoSequenceSchema(schema, fromDataAnnotation);
return name == null
? null
: propertyBuilder.Metadata.DeclaringType.Model.Builder.HasSequence(name, schema, fromDataAnnotation);
}
/// <summary>
/// Returns a value indicating whether the given name and schema can be set for the hi-lo sequence.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given name and schema can be set for the hi-lo sequence.</returns>
public static bool CanSetHiLoSequence(
this IConventionPropertyBuilder propertyBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
return propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.HiLoSequenceName, name, fromDataAnnotation)
&& propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.HiLoSequenceSchema, schema, fromDataAnnotation);
}
/// <summary>
/// Configures the key property to use a sequence-based key value generation pattern to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder UseSequence(
this PropertyBuilder propertyBuilder,
string? name = null,
string? schema = null)
{
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
var property = propertyBuilder.Metadata;
property.SetValueGenerationStrategy(SqlServerValueGenerationStrategy.Sequence);
property.SetSequenceName(name);
property.SetSequenceSchema(schema);
property.SetHiLoSequenceName(null);
property.SetHiLoSequenceSchema(null);
property.SetIdentitySeed(null);
property.SetIdentityIncrement(null);
return propertyBuilder;
}
/// <summary>
/// Configures the key property to use a sequence-based key value generation pattern to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TProperty> UseSequence<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
string? name = null,
string? schema = null)
=> (PropertyBuilder<TProperty>)UseSequence((PropertyBuilder)propertyBuilder, name, schema);
/// <summary>
/// Configures the database sequence used for the key value generation pattern to generate values for the key property,
/// when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>A builder to further configure the sequence.</returns>
public static IConventionSequenceBuilder? HasSequence(
this IConventionPropertyBuilder propertyBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
if (!propertyBuilder.CanSetSequence(name, schema, fromDataAnnotation))
{
return null;
}
propertyBuilder.Metadata.SetSequenceName(name, fromDataAnnotation);
propertyBuilder.Metadata.SetSequenceSchema(schema, fromDataAnnotation);
return name == null
? null
: propertyBuilder.Metadata.DeclaringType.Model.Builder.HasSequence(name, schema, fromDataAnnotation);
}
/// <summary>
/// Returns a value indicating whether the given name and schema can be set for the key value generation sequence.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="name">The name of the sequence.</param>
/// <param name="schema">The schema of the sequence.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given name and schema can be set for the key value generation sequence.</returns>
public static bool CanSetSequence(
this IConventionPropertyBuilder propertyBuilder,
string? name,
string? schema,
bool fromDataAnnotation = false)
{
Check.NullButNotEmpty(name, nameof(name));
Check.NullButNotEmpty(schema, nameof(schema));
return propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.SequenceName, name, fromDataAnnotation)
&& propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.SequenceSchema, schema, fromDataAnnotation);
}
/// <summary>
/// Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder UseIdentityColumn(
this PropertyBuilder propertyBuilder,
long seed = 1,
int increment = 1)
{
var property = propertyBuilder.Metadata;
property.SetValueGenerationStrategy(SqlServerValueGenerationStrategy.IdentityColumn);
property.SetIdentitySeed(seed);
property.SetIdentityIncrement(increment);
property.SetHiLoSequenceName(null);
property.SetHiLoSequenceSchema(null);
property.SetSequenceName(null);
property.SetSequenceSchema(null);
return propertyBuilder;
}
/// <summary>
/// Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder UseIdentityColumn(
this PropertyBuilder propertyBuilder,
int seed,
int increment = 1)
=> propertyBuilder.UseIdentityColumn((long)seed, increment);
/// <summary>
/// Configures the key column to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="columnBuilder">The builder for the column being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ColumnBuilder UseIdentityColumn(
this ColumnBuilder columnBuilder,
long seed = 1,
int increment = 1)
{
var overrides = columnBuilder.Overrides;
overrides.SetValueGenerationStrategy(SqlServerValueGenerationStrategy.IdentityColumn);
overrides.SetIdentitySeed(seed);
overrides.SetIdentityIncrement(increment);
return columnBuilder;
}
/// <summary>
/// Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TProperty> UseIdentityColumn<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
long seed = 1,
int increment = 1)
=> (PropertyBuilder<TProperty>)UseIdentityColumn((PropertyBuilder)propertyBuilder, seed, increment);
/// <summary>
/// Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static PropertyBuilder<TProperty> UseIdentityColumn<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
int seed,
int increment = 1)
=> (PropertyBuilder<TProperty>)UseIdentityColumn((PropertyBuilder)propertyBuilder, (long)seed, increment);
/// <summary>
/// Configures the key column to use the SQL Server IDENTITY feature to generate values for new entities,
/// when targeting SQL Server. This method sets the property to be <see cref="ValueGenerated.OnAdd" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
/// <param name="columnBuilder">The builder for the column being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static ColumnBuilder<TProperty> UseIdentityColumn<TProperty>(
this ColumnBuilder<TProperty> columnBuilder,
long seed = 1,
int increment = 1)
=> (ColumnBuilder<TProperty>)UseIdentityColumn((ColumnBuilder)columnBuilder, seed, increment);
/// <summary>
/// Configures the seed for SQL Server IDENTITY.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasIdentityColumnSeed(
this IConventionPropertyBuilder propertyBuilder,
long? seed,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetIdentityColumnSeed(seed, fromDataAnnotation))
{
propertyBuilder.Metadata.SetIdentitySeed(seed, fromDataAnnotation);
return propertyBuilder;
}
return null;
}
/// <summary>
/// Configures the seed for SQL Server IDENTITY for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasIdentityColumnSeed(
this IConventionPropertyBuilder propertyBuilder,
long? seed,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetIdentityColumnSeed(seed, storeObject, fromDataAnnotation))
{
propertyBuilder.Metadata.SetIdentitySeed(seed, storeObject, fromDataAnnotation);
return propertyBuilder;
}
return null;
}
/// <summary>
/// Returns a value indicating whether the given value can be set as the seed for SQL Server IDENTITY.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the seed for SQL Server IDENTITY.</returns>
public static bool CanSetIdentityColumnSeed(
this IConventionPropertyBuilder propertyBuilder,
long? seed,
bool fromDataAnnotation = false)
=> propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.IdentitySeed, seed, fromDataAnnotation);
/// <summary>
/// Returns a value indicating whether the given value can be set as the seed for SQL Server IDENTITY
/// for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="seed">The value that is used for the very first row loaded into the table.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the seed for SQL Server IDENTITY.</returns>
public static bool CanSetIdentityColumnSeed(
this IConventionPropertyBuilder propertyBuilder,
long? seed,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
=> propertyBuilder.Metadata.FindOverrides(storeObject)?.Builder
.CanSetAnnotation(
SqlServerAnnotationNames.IdentitySeed,
seed,
fromDataAnnotation)
?? true;
/// <summary>
/// Configures the increment for SQL Server IDENTITY.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasIdentityColumnIncrement(
this IConventionPropertyBuilder propertyBuilder,
int? increment,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetIdentityColumnIncrement(increment, fromDataAnnotation))
{
propertyBuilder.Metadata.SetIdentityIncrement(increment, fromDataAnnotation);
return propertyBuilder;
}
return null;
}
/// <summary>
/// Configures the increment for SQL Server IDENTITY for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasIdentityColumnIncrement(
this IConventionPropertyBuilder propertyBuilder,
int? increment,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetIdentityColumnIncrement(increment, storeObject, fromDataAnnotation))
{
propertyBuilder.Metadata.SetIdentityIncrement(increment, storeObject, fromDataAnnotation);
return propertyBuilder;
}
return null;
}
/// <summary>
/// Returns a value indicating whether the given value can be set as the increment for SQL Server IDENTITY.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default increment for SQL Server IDENTITY.</returns>
public static bool CanSetIdentityColumnIncrement(
this IConventionPropertyBuilder propertyBuilder,
int? increment,
bool fromDataAnnotation = false)
=> propertyBuilder.CanSetAnnotation(SqlServerAnnotationNames.IdentityIncrement, increment, fromDataAnnotation);
/// <summary>
/// Returns a value indicating whether the given value can be set as the increment for SQL Server IDENTITY
/// for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="increment">The incremental value that is added to the identity value of the previous row that was loaded.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default increment for SQL Server IDENTITY.</returns>
public static bool CanSetIdentityColumnIncrement(
this IConventionPropertyBuilder propertyBuilder,
int? increment,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
=> propertyBuilder.Metadata.FindOverrides(storeObject)?.Builder
.CanSetAnnotation(
SqlServerAnnotationNames.IdentityIncrement,
increment,
fromDataAnnotation)
?? true;
/// <summary>
/// Configures the value generation strategy for the key property, when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="valueGenerationStrategy">The value generation strategy.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasValueGenerationStrategy(
this IConventionPropertyBuilder propertyBuilder,
SqlServerValueGenerationStrategy? valueGenerationStrategy,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetAnnotation(
SqlServerAnnotationNames.ValueGenerationStrategy, valueGenerationStrategy, fromDataAnnotation))
{
propertyBuilder.Metadata.SetValueGenerationStrategy(valueGenerationStrategy, fromDataAnnotation);
if (valueGenerationStrategy != SqlServerValueGenerationStrategy.IdentityColumn)
{
propertyBuilder.HasIdentityColumnSeed(null, fromDataAnnotation);
propertyBuilder.HasIdentityColumnIncrement(null, fromDataAnnotation);
propertyBuilder.HasSequence(null, null, fromDataAnnotation);
}
if (valueGenerationStrategy != SqlServerValueGenerationStrategy.SequenceHiLo)
{
propertyBuilder.HasHiLoSequence(null, null, fromDataAnnotation);
propertyBuilder.HasSequence(null, null, fromDataAnnotation);
}
if (valueGenerationStrategy != SqlServerValueGenerationStrategy.Sequence)
{
propertyBuilder.HasIdentityColumnSeed(null, fromDataAnnotation);
propertyBuilder.HasIdentityColumnIncrement(null, fromDataAnnotation);
propertyBuilder.HasHiLoSequence(null, null, fromDataAnnotation);
}
return propertyBuilder;
}
return null;
}
/// <summary>
/// Configures the value generation strategy for the key property, when targeting SQL Server for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="valueGenerationStrategy">The value generation strategy.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionPropertyBuilder? HasValueGenerationStrategy(
this IConventionPropertyBuilder propertyBuilder,
SqlServerValueGenerationStrategy? valueGenerationStrategy,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetValueGenerationStrategy(valueGenerationStrategy, storeObject, fromDataAnnotation))
{
propertyBuilder.Metadata.SetValueGenerationStrategy(valueGenerationStrategy, storeObject, fromDataAnnotation);
if (valueGenerationStrategy != SqlServerValueGenerationStrategy.IdentityColumn)
{
propertyBuilder.HasIdentityColumnSeed(null, storeObject, fromDataAnnotation);
propertyBuilder.HasIdentityColumnIncrement(null, storeObject, fromDataAnnotation);
}
return propertyBuilder;
}
return null;
}
/// <summary>
/// Returns a value indicating whether the given value can be set as the value generation strategy.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="valueGenerationStrategy">The value generation strategy.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default value generation strategy.</returns>
public static bool CanSetValueGenerationStrategy(
this IConventionPropertyBuilder propertyBuilder,
SqlServerValueGenerationStrategy? valueGenerationStrategy,
bool fromDataAnnotation = false)
=> propertyBuilder.CanSetAnnotation(
SqlServerAnnotationNames.ValueGenerationStrategy, valueGenerationStrategy, fromDataAnnotation);
/// <summary>
/// Returns a value indicating whether the given value can be set as the value generation strategy for a particular table.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="valueGenerationStrategy">The value generation strategy.</param>
/// <param name="storeObject">The table identifier.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the given value can be set as the default value generation strategy.</returns>
public static bool CanSetValueGenerationStrategy(
this IConventionPropertyBuilder propertyBuilder,
SqlServerValueGenerationStrategy? valueGenerationStrategy,
in StoreObjectIdentifier storeObject,
bool fromDataAnnotation = false)
=> propertyBuilder.Metadata.FindOverrides(storeObject)?.Builder
.CanSetAnnotation(
SqlServerAnnotationNames.ValueGenerationStrategy,
valueGenerationStrategy,
fromDataAnnotation)
?? true;
/// <summary>
/// Configures whether the property's column is created as sparse when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples. Also see
/// <see href="https://docs.microsoft.com/sql/relational-databases/tables/use-sparse-columns">Sparse columns</see> for
/// general information on SQL Server sparse columns.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="sparse">A value indicating whether the property's column is created as sparse.</param>
/// <returns>A builder to further configure the property.</returns>
public static PropertyBuilder IsSparse(this PropertyBuilder propertyBuilder, bool sparse = true)
{
propertyBuilder.Metadata.SetIsSparse(sparse);
return propertyBuilder;
}
/// <summary>
/// Configures whether the property's column is created as sparse when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples. Also see
/// <see href="https://docs.microsoft.com/sql/relational-databases/tables/use-sparse-columns">Sparse columns</see> for
/// general information on SQL Server sparse columns.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="sparse">A value indicating whether the property's column is created as sparse.</param>
/// <returns>A builder to further configure the property.</returns>
public static PropertyBuilder<TProperty> IsSparse<TProperty>(
this PropertyBuilder<TProperty> propertyBuilder,
bool sparse = true)
=> (PropertyBuilder<TProperty>)IsSparse((PropertyBuilder)propertyBuilder, sparse);
/// <summary>
/// Configures whether the property's column is created as sparse when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples. Also see
/// <see href="https://docs.microsoft.com/sql/relational-databases/tables/use-sparse-columns">Sparse columns</see> for
/// general information on SQL Server sparse columns.
/// </remarks>
/// <param name="propertyBuilder">The builder for the property being configured.</param>
/// <param name="sparse">A value indicating whether the property's column is created as sparse.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance if the configuration was applied, <see langword="null" /> otherwise.</returns>
public static IConventionPropertyBuilder? IsSparse(
this IConventionPropertyBuilder propertyBuilder,
bool? sparse,
bool fromDataAnnotation = false)
{
if (propertyBuilder.CanSetIsSparse(sparse, fromDataAnnotation))
{
propertyBuilder.Metadata.SetIsSparse(sparse, fromDataAnnotation);
return propertyBuilder;
}
return null;
}
/// <summary>
/// Returns a value indicating whether the property's column can be configured as sparse when targeting SQL Server.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-sqlserver">Accessing SQL Server and Azure SQL databases with EF Core</see>
/// for more information and examples. Also see
/// <see href="https://docs.microsoft.com/sql/relational-databases/tables/use-sparse-columns">Sparse columns</see> for
/// general information on SQL Server sparse columns.
/// </remarks>
/// <param name="property">The builder for the property being configured.</param>
/// <param name="sparse">A value indicating whether the property's column is created as sparse.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>The same builder instance if the configuration was applied, <see langword="null" /> otherwise.</returns>
/// <returns>
/// <see langword="true" /> if the property's column can be configured as sparse when targeting SQL Server.
/// </returns>
public static bool CanSetIsSparse(
this IConventionPropertyBuilder property,
bool? sparse,
bool fromDataAnnotation = false)
=> property.CanSetAnnotation(SqlServerAnnotationNames.Sparse, sparse, fromDataAnnotation);
}