/
DbContext.cs
801 lines (708 loc) · 38.9 KB
/
DbContext.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// A DbContext instance represents a session with the database and can be used to query and save
/// instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.
/// </summary>
/// <remarks>
/// <para>
/// Typically you create a class that derives from DbContext and contains <see cref="DbSet{TEntity}" />
/// properties for each entity in the model. If the <see cref="DbSet{TEntity}" /> properties have a public setter,
/// they are automatically initialized when the instance of the derived context is created.
/// </para>
/// <para>
/// Override the <see cref="OnConfiguring(DbContextOptionsBuilder)" /> method to configure the database (and
/// other options) to be used for the context. Alternatively, if you would rather perform configuration externally
/// instead of inline in your context, you can use <see cref="DbContextOptionsBuilder{TContext}" />
/// (or <see cref="DbContextOptionsBuilder" />) to externally create an instance of <see cref="DbContextOptions{TContext}" />
/// (or <see cref="DbContextOptions" />) and pass it to a base constructor of <see cref="DbContext" />.
/// </para>
/// <para>
/// The model is discovered by running a set of conventions over the entity classes found in the
/// <see cref="DbSet{TEntity}" /> properties on the derived context. To further configure the model that
/// is discovered by convention, you can override the <see cref="OnModelCreating(ModelBuilder)" /> method.
/// </para>
/// </remarks>
public class DbContext : IDisposable, IInfrastructure<IServiceProvider>
{
private readonly DbContextOptions _options;
private IDbContextServices _contextServices;
private IDbSetInitializer _setInitializer;
private ChangeTracker _changeTracker;
private IStateManager _stateManager;
private IChangeDetector _changeDetector;
private IEntityGraphAttacher _graphAttacher;
private IModel _model;
private ILogger _logger;
private bool _initializing;
private IServiceScope _serviceScope;
private bool _disposed;
/// <summary>
/// <para>
/// Initializes a new instance of the <see cref="DbContext" /> class. The
/// <see cref="OnConfiguring(DbContextOptionsBuilder)" />
/// method will be called to configure the database (and other options) to be used for this context.
/// </para>
/// </summary>
protected DbContext()
: this(new DbContextOptions<DbContext>())
{
}
/// <summary>
/// <para>
/// Initializes a new instance of the <see cref="DbContext" /> class using the specified options.
/// The <see cref="OnConfiguring(DbContextOptionsBuilder)" /> method will still be called to allow further
/// configuration of the options.
/// </para>
/// </summary>
/// <param name="options">The options for this context.</param>
public DbContext([NotNull] DbContextOptions options)
{
Check.NotNull(options, nameof(options));
if (!options.ContextType.GetTypeInfo().IsAssignableFrom(GetType().GetTypeInfo()))
{
throw new InvalidOperationException(CoreStrings.NonGenericOptions(GetType().DisplayName()));
}
_options = options;
InitializeSets(ServiceProviderCache.Instance.GetOrAdd(options));
}
private IChangeDetector ChangeDetector
=> _changeDetector
?? (_changeDetector = InternalServiceProvider.GetRequiredService<IChangeDetector>());
private IStateManager StateManager
=> _stateManager
?? (_stateManager = InternalServiceProvider.GetRequiredService<IStateManager>());
private IServiceProvider InternalServiceProvider
{
get
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().Name, CoreStrings.ContextDisposed);
}
return (_contextServices ?? (_contextServices = InitializeServices())).InternalServiceProvider;
}
}
private IDbContextServices InitializeServices()
{
if (_initializing)
{
throw new InvalidOperationException(CoreStrings.RecursiveOnConfiguring);
}
try
{
_initializing = true;
var optionsBuilder = new DbContextOptionsBuilder(_options);
OnConfiguring(optionsBuilder);
var options = optionsBuilder.Options;
var serviceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider
?? ServiceProviderCache.Instance.GetOrAdd(options);
_serviceScope = serviceProvider
.GetRequiredService<IServiceScopeFactory>()
.CreateScope();
var scopedServiceProvider = _serviceScope.ServiceProvider;
var contextServices = scopedServiceProvider.GetService<IDbContextServices>();
if (contextServices == null)
{
throw new InvalidOperationException(CoreStrings.NoEfServices);
}
contextServices.Initialize(scopedServiceProvider, options, this);
_logger = scopedServiceProvider.GetRequiredService<ILogger<DbContext>>();
return contextServices;
}
finally
{
_initializing = false;
}
}
private void InitializeSets(IServiceProvider internalServicesProvider)
=> internalServicesProvider.GetRequiredService<IDbSetInitializer>().InitializeSets(this);
/// <summary>
/// <para>
/// Gets the scoped <see cref="IServiceProvider" /> being used to resolve services.
/// </para>
/// <para>
/// This property is intended for use by extension methods that need to make use of services
/// not directly exposed in the public API surface.
/// </para>
/// </summary>
IServiceProvider IInfrastructure<IServiceProvider>.Instance => InternalServiceProvider;
/// <summary>
/// <para>
/// Override this method to configure the database (and other options) to be used for this context.
/// This method is called for each instance of the context that is created.
/// </para>
/// <para>
/// In situations where an instance of <see cref="DbContextOptions" /> may or may not have been passed
/// to the constructor, you can use <see cref="DbContextOptionsBuilder.IsConfigured" /> to determine if
/// the options have already been set, and skip some or all of the logic in
/// <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)" />.
/// </para>
/// </summary>
/// <param name="optionsBuilder">
/// A builder used to create or modify options for this context. Databases (and other extensions)
/// typically define extension methods on this object that allow you to configure the context.
/// </param>
protected internal virtual void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
/// <summary>
/// Override this method to further configure the model that was discovered by convention from the entity types
/// exposed in <see cref="DbSet{TEntity}" /> properties on your derived context. The resulting model may be cached
/// and re-used for subsequent instances of your derived context.
/// </summary>
/// <remarks>
/// If a model is explicitly set on the options for this context (via <see cref="DbContextOptionsBuilder.UseModel(IModel)" />)
/// then this method will not be run.
/// </remarks>
/// <param name="modelBuilder">
/// The builder being used to construct the model for this context. Databases (and other extensions) typically
/// define extension methods on this object that allow you to configure aspects of the model that are specific
/// to a given database.
/// </param>
protected internal virtual void OnModelCreating(ModelBuilder modelBuilder)
{
}
/// <summary>
/// Saves all changes made in this context to the database.
/// </summary>
/// <remarks>
/// This method will automatically call <see cref="ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </remarks>
/// <returns>
/// The number of state entries written to the database.
/// </returns>
[DebuggerStepThrough]
public virtual int SaveChanges() => SaveChanges(acceptAllChangesOnSuccess: true);
/// <summary>
/// Saves all changes made in this context to the database.
/// </summary>
/// <param name="acceptAllChangesOnSuccess">
/// Indicates whether <see cref="ChangeTracking.ChangeTracker.AcceptAllChanges" /> is called after the changes have
/// been sent successfully to the database.
/// </param>
/// <remarks>
/// This method will automatically call <see cref="ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </remarks>
/// <returns>
/// The number of state entries written to the database.
/// </returns>
[DebuggerStepThrough]
public virtual int SaveChanges(bool acceptAllChangesOnSuccess)
{
var stateManager = StateManager;
TryDetectChanges(stateManager);
try
{
return stateManager.SaveChanges(acceptAllChangesOnSuccess);
}
catch (Exception exception)
{
_logger.LogError(
CoreEventId.DatabaseError,
() => new DatabaseErrorLogState(GetType()),
exception,
e => CoreStrings.LogExceptionDuringSaveChanges(Environment.NewLine, e));
throw;
}
}
private void TryDetectChanges(IStateManager stateManager)
{
if (ChangeTracker.AutoDetectChangesEnabled)
{
ChangeDetector.DetectChanges(stateManager);
}
}
/// <summary>
/// Asynchronously saves all changes made in this context to the database.
/// </summary>
/// <remarks>
/// <para>
/// This method will automatically call <see cref="ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </para>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use 'await' to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// </para>
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>
/// A task that represents the asynchronous save operation. The task result contains the
/// number of state entries written to the database.
/// </returns>
public virtual Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
=> SaveChangesAsync(acceptAllChangesOnSuccess: true, cancellationToken: cancellationToken);
/// <summary>
/// Asynchronously saves all changes made in this context to the database.
/// </summary>
/// <param name="acceptAllChangesOnSuccess">
/// Indicates whether <see cref="ChangeTracking.ChangeTracker.AcceptAllChanges" /> is called after the changes have
/// been sent successfully to the database.
/// </param>
/// <remarks>
/// <para>
/// This method will automatically call <see cref="ChangeTracking.ChangeTracker.DetectChanges" /> to discover any
/// changes to entity instances before saving to the underlying database. This can be disabled via
/// <see cref="ChangeTracking.ChangeTracker.AutoDetectChangesEnabled" />.
/// </para>
/// <para>
/// Multiple active operations on the same context instance are not supported. Use 'await' to ensure
/// that any asynchronous operations have completed before calling another method on this context.
/// </para>
/// </remarks>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
/// <returns>
/// A task that represents the asynchronous save operation. The task result contains the
/// number of state entries written to the database.
/// </returns>
public virtual async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess,
CancellationToken cancellationToken = default(CancellationToken))
{
var stateManager = StateManager;
if (ChangeTracker.AutoDetectChangesEnabled)
{
ChangeDetector.DetectChanges(stateManager);
}
try
{
return await stateManager.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
catch (Exception exception)
{
_logger.LogError(
CoreEventId.DatabaseError,
() => new DatabaseErrorLogState(GetType()),
exception,
e => CoreStrings.LogExceptionDuringSaveChanges(Environment.NewLine, e));
throw;
}
}
/// <summary>
/// Releases the allocated resources for this context.
/// </summary>
public virtual void Dispose()
{
if (!_disposed)
{
_disposed = true;
_stateManager?.Unsubscribe();
_serviceScope?.Dispose();
_setInitializer = null;
_changeTracker = null;
_stateManager = null;
_changeDetector = null;
_graphAttacher = null;
_model = null;
}
}
/// <summary>
/// Gets an <see cref="EntityEntry{TEntity}" /> for the given entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </summary>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <param name="entity"> The entity to get the entry for. </param>
/// <returns> The entry for the given entity. </returns>
public virtual EntityEntry<TEntity> Entry<TEntity>([NotNull] TEntity entity) where TEntity : class
{
Check.NotNull(entity, nameof(entity));
TryDetectChanges(StateManager);
return EntryWithoutDetectChanges(entity);
}
private EntityEntry<TEntity> EntryWithoutDetectChanges<TEntity>(TEntity entity) where TEntity : class
=> new EntityEntry<TEntity>(StateManager.GetOrCreateEntry(entity));
/// <summary>
/// <para>
/// Gets an <see cref="EntityEntry" /> for the given entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </para>
/// <para>
/// This method may be called on an entity that is not tracked. You can then
/// set the <see cref="EntityEntry.State" /> property on the returned entry
/// to have the context begin tracking the entity in the specified state.
/// </para>
/// </summary>
/// <param name="entity"> The entity to get the entry for. </param>
/// <returns> The entry for the given entity. </returns>
public virtual EntityEntry Entry([NotNull] object entity)
{
Check.NotNull(entity, nameof(entity));
TryDetectChanges(StateManager);
return EntryWithoutDetectChanges(entity);
}
private EntityEntry EntryWithoutDetectChanges(object entity)
=> new EntityEntry(StateManager.GetOrCreateEntry(entity));
private void SetEntityState(InternalEntityEntry entry, EntityState entityState)
{
if (entry.EntityState == EntityState.Detached)
{
(_graphAttacher
?? (_graphAttacher = InternalServiceProvider.GetRequiredService<IEntityGraphAttacher>()))
.AttachGraph(entry, entityState);
}
else
{
entry.SetEntityState(entityState, acceptChanges: true);
}
}
/// <summary>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Added" /> state such that
/// they will be inserted into the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <param name="entity"> The entity to add. </param>
/// <returns>
/// The <see cref="EntityEntry{TEntity}" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry<TEntity> Add<TEntity>([NotNull] TEntity entity) where TEntity : class
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Added);
/// <summary>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Unchanged" /> state such that no
/// operation will be performed when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <param name="entity"> The entity to attach. </param>
/// <returns>
/// The <see cref="EntityEntry{TEntity}" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry<TEntity> Attach<TEntity>([NotNull] TEntity entity) where TEntity : class
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Unchanged);
/// <summary>
/// <para>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Modified" /> state such that they will
/// be updated in the database when <see cref="SaveChanges()" /> is called.
/// </para>
/// <para>
/// All properties of the entity will be marked as modified. To mark only some properties as modified, use
/// <see cref="Attach{TEntity}(TEntity)" /> to begin tracking the entity in the
/// <see cref="EntityState.Unchanged" /> state and then use the returned <see cref="EntityEntry{TEntity}" />
/// to mark the desired properties as modified.
/// </para>
/// </summary>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <param name="entity"> The entity to update. </param>
/// <returns>
/// The <see cref="EntityEntry{TEntity}" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry<TEntity> Update<TEntity>([NotNull] TEntity entity) where TEntity : class
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Modified);
/// <summary>
/// Begins tracking the given entity in the <see cref="EntityState.Deleted" /> state such that it will
/// be removed from the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <remarks>
/// <para>
/// If the entity is already tracked in the <see cref="EntityState.Added" /> state then the context will
/// stop tracking the entity (rather than marking it as <see cref="EntityState.Deleted" />) since the
/// entity was previously added to the context and does not exist in the database.
/// </para>
/// <para>
/// Any other reachable entities that are not already being tracked will be tracked in the same way that
/// they would be if <see cref="Attach{TEntity}(TEntity)" /> was called before calling this method.
/// This allows any cascading actions to be applied when <see cref="SaveChanges()" /> is called.
/// </para>
/// </remarks>
/// <typeparam name="TEntity"> The type of the entity. </typeparam>
/// <param name="entity"> The entity to remove. </param>
/// <returns>
/// The <see cref="EntityEntry{TEntity}" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry<TEntity> Remove<TEntity>([NotNull] TEntity entity) where TEntity : class
{
Check.NotNull(entity, nameof(entity));
var entry = EntryWithoutDetectChanges(entity);
var initialState = entry.State;
if (initialState == EntityState.Detached)
{
SetEntityState(entry.GetInfrastructure(), EntityState.Unchanged);
}
// An Added entity does not yet exist in the database. If it is then marked as deleted there is
// nothing to delete because it was not yet inserted, so just make sure it doesn't get inserted.
entry.State =
initialState == EntityState.Added
? EntityState.Detached
: EntityState.Deleted;
return entry;
}
private EntityEntry<TEntity> SetEntityState<TEntity>(
TEntity entity,
EntityState entityState) where TEntity : class
{
var entry = EntryWithoutDetectChanges(entity);
SetEntityState(entry.GetInfrastructure(), entityState);
return entry;
}
/// <summary>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Added" /> state such that they will
/// be inserted into the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entity"> The entity to add. </param>
/// <returns>
/// The <see cref="EntityEntry" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry Add([NotNull] object entity)
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Added);
/// <summary>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Unchanged" /> state such that no
/// operation will be performed when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entity"> The entity to attach. </param>
/// <returns>
/// The <see cref="EntityEntry" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry Attach([NotNull] object entity)
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Unchanged);
/// <summary>
/// <para>
/// Begins tracking the given entity, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Modified" /> state such that they will
/// be updated in the database when <see cref="SaveChanges()" /> is called.
/// </para>
/// <para>
/// All properties of the entity will be marked as modified. To mark only some properties as modified, use
/// <see cref="Attach(object)" /> to begin tracking the entity in the <see cref="EntityState.Unchanged" />
/// state and then use the returned <see cref="EntityEntry" /> to mark the desired properties as modified.
/// </para>
/// </summary>
/// <param name="entity"> The entity to update. </param>
/// <returns>
/// The <see cref="EntityEntry" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry Update([NotNull] object entity)
=> SetEntityState(Check.NotNull(entity, nameof(entity)), EntityState.Modified);
/// <summary>
/// Begins tracking the given entity in the <see cref="EntityState.Deleted" /> state such that it will
/// be removed from the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <remarks>
/// <para>
/// If the entity is already tracked in the <see cref="EntityState.Added" /> state then the context will
/// stop tracking the entity (rather than marking it as <see cref="EntityState.Deleted" />) since the
/// entity was previously added to the context and does not exist in the database.
/// </para>
/// <para>
/// Any other reachable entities that are not already being tracked will be tracked in the same way that
/// they would be if <see cref="Attach(object)" /> was called before calling this method.
/// This allows any cascading actions to be applied when <see cref="SaveChanges()" /> is called.
/// </para>
/// </remarks>
/// <param name="entity"> The entity to remove. </param>
/// <returns>
/// The <see cref="EntityEntry" /> for the entity. The entry provides
/// access to change tracking information and operations for the entity.
/// </returns>
public virtual EntityEntry Remove([NotNull] object entity)
{
Check.NotNull(entity, nameof(entity));
var entry = EntryWithoutDetectChanges(entity);
var initialState = entry.State;
if (initialState == EntityState.Detached)
{
SetEntityState(entry.GetInfrastructure(), EntityState.Unchanged);
}
// An Added entity does not yet exist in the database. If it is then marked as deleted there is
// nothing to delete because it was not yet inserted, so just make sure it doesn't get inserted.
entry.State =
initialState == EntityState.Added
? EntityState.Detached
: EntityState.Deleted;
return entry;
}
private EntityEntry SetEntityState(object entity, EntityState entityState)
{
var entry = EntryWithoutDetectChanges(entity);
SetEntityState(entry.GetInfrastructure(), entityState);
return entry;
}
/// <summary>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Added" /> state such that they will
/// be inserted into the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entities"> The entities to add. </param>
public virtual void AddRange([NotNull] params object[] entities)
=> AddRange((IEnumerable<object>)entities);
/// <summary>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Unchanged" /> state such that no
/// operation will be performed when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entities"> The entities to attach. </param>
public virtual void AttachRange([NotNull] params object[] entities)
=> AttachRange((IEnumerable<object>)entities);
/// <summary>
/// <para>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Modified" /> state such that they will
/// be updated in the database when <see cref="SaveChanges()" /> is called.
/// </para>
/// <para>
/// All properties of the entities will be marked as modified. To mark only some properties as modified, use
/// <see cref="Attach(object)" /> to begin tracking each entity in the <see cref="EntityState.Unchanged" />
/// state and then use the returned <see cref="EntityEntry" /> to mark the desired properties as modified.
/// </para>
/// </summary>
/// <param name="entities"> The entities to update. </param>
public virtual void UpdateRange([NotNull] params object[] entities)
=> UpdateRange((IEnumerable<object>)entities);
/// <summary>
/// Begins tracking the given entity in the <see cref="EntityState.Deleted" /> state such that it will
/// be removed from the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <remarks>
/// <para>
/// If any of the entities are already tracked in the <see cref="EntityState.Added" /> state then the context will
/// stop tracking those entities (rather than marking them as <see cref="EntityState.Deleted" />) since those
/// entities were previously added to the context and do not exist in the database.
/// </para>
/// <para>
/// Any other reachable entities that are not already being tracked will be tracked in the same way that
/// they would be if <see cref="AttachRange(object[])" /> was called before calling this method.
/// This allows any cascading actions to be applied when <see cref="SaveChanges()" /> is called.
/// </para>
/// </remarks>
/// <param name="entities"> The entities to remove. </param>
public virtual void RemoveRange([NotNull] params object[] entities)
=> RemoveRange((IEnumerable<object>)entities);
private void SetEntityStates(IEnumerable<object> entities, EntityState entityState)
{
var stateManager = StateManager;
foreach (var entity in entities)
{
SetEntityState(stateManager.GetOrCreateEntry(entity), entityState);
}
}
/// <summary>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Added" /> state such that they will
/// be inserted into the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entities"> The entities to add. </param>
public virtual void AddRange([NotNull] IEnumerable<object> entities)
=> SetEntityStates(Check.NotNull(entities, nameof(entities)), EntityState.Added);
/// <summary>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Unchanged" /> state such that no
/// operation will be performed when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <param name="entities"> The entities to attach. </param>
public virtual void AttachRange([NotNull] IEnumerable<object> entities)
=> SetEntityStates(Check.NotNull(entities, nameof(entities)), EntityState.Unchanged);
/// <summary>
/// <para>
/// Begins tracking the given entities, and any other reachable entities that are
/// not already being tracked, in the <see cref="EntityState.Modified" /> state such that they will
/// be updated in the database when <see cref="SaveChanges()" /> is called.
/// </para>
/// <para>
/// All properties of the entities will be marked as modified. To mark only some properties as modified, use
/// <see cref="Attach(object)" /> to begin tracking each entity in the <see cref="EntityState.Unchanged" />
/// state and then use the returned <see cref="EntityEntry" /> to mark the desired properties as modified.
/// </para>
/// </summary>
/// <param name="entities"> The entities to update. </param>
public virtual void UpdateRange([NotNull] IEnumerable<object> entities)
=> SetEntityStates(Check.NotNull(entities, nameof(entities)), EntityState.Modified);
/// <summary>
/// Begins tracking the given entity in the <see cref="EntityState.Deleted" /> state such that it will
/// be removed from the database when <see cref="SaveChanges()" /> is called.
/// </summary>
/// <remarks>
/// <para>
/// If any of the entities are already tracked in the <see cref="EntityState.Added" /> state then the context will
/// stop tracking those entities (rather than marking them as <see cref="EntityState.Deleted" />) since those
/// entities were previously added to the context and do not exist in the database.
/// </para>
/// <para>
/// Any other reachable entities that are not already being tracked will be tracked in the same way that
/// they would be if <see cref="AttachRange(IEnumerable{object})" /> was called before calling this method.
/// This allows any cascading actions to be applied when <see cref="SaveChanges()" /> is called.
/// </para>
/// </remarks>
/// <param name="entities"> The entities to remove. </param>
public virtual void RemoveRange([NotNull] IEnumerable<object> entities)
{
Check.NotNull(entities, nameof(entities));
var stateManager = StateManager;
// An Added entity does not yet exist in the database. If it is then marked as deleted there is
// nothing to delete because it was not yet inserted, so just make sure it doesn't get inserted.
foreach (var entity in entities)
{
var entry = stateManager.GetOrCreateEntry(entity);
var initialState = entry.EntityState;
if (initialState == EntityState.Detached)
{
SetEntityState(entry, EntityState.Unchanged);
}
entry.SetEntityState(initialState == EntityState.Added
? EntityState.Detached
: EntityState.Deleted);
}
}
/// <summary>
/// Provides access to database related information and operations for this context.
/// </summary>
public virtual DatabaseFacade Database => new DatabaseFacade(this);
/// <summary>
/// Provides access to information and operations for entity instances this context is tracking.
/// </summary>
public virtual ChangeTracker ChangeTracker
=> _changeTracker
?? (_changeTracker = InternalServiceProvider.GetRequiredService<IChangeTrackerFactory>().Create());
/// <summary>
/// The metadata about the shape of entities, the relationships between them, and how they map to the database.
/// </summary>
public virtual IModel Model
=> _model
?? (_model = InternalServiceProvider.GetRequiredService<IModel>());
/// <summary>
/// Creates a <see cref="DbSet{TEntity}" /> that can be used to query and save instances of <typeparamref name="TEntity" />.
/// </summary>
/// <typeparam name="TEntity"> The type of entity for which a set should be returned. </typeparam>
/// <returns> A set for the given entity type. </returns>
public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class
{
if (Model.FindEntityType(typeof(TEntity)) == null)
{
throw new InvalidOperationException(CoreStrings.InvalidSetType(typeof(TEntity).Name));
}
return (_setInitializer
?? (_setInitializer = InternalServiceProvider.GetRequiredService<IDbSetInitializer>())).CreateSet<TEntity>(this);
}
}
}