Repository navigation
Expand file tree
/
Copy pathDataContext.cs
More file actions
1707 lines (1567 loc) · 70.9 KB
/
Copy pathDataContext.cs
File metadata and controls
1707 lines (1567 loc) · 70.9 KB
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Transactions;
using System.Xml;
using System.Runtime.CompilerServices;
namespace System.Data.Linq {
using System.Data.Linq.Mapping;
using System.Data.Linq.Provider;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Used to specify how a submit should behave when one
/// or more updates fail due to optimistic concurrency
/// conflicts.
/// </summary>
public enum ConflictMode {
/// <summary>
/// Fail immediately when the first change conflict is encountered.
/// </summary>
FailOnFirstConflict,
/// <summary>
/// Only fail after all changes have been attempted.
/// </summary>
ContinueOnConflict
}
/// <summary>
/// Used to specify a value synchronization strategy.
/// </summary>
public enum RefreshMode {
/// <summary>
/// Keep the current values.
/// </summary>
KeepCurrentValues,
/// <summary>
/// Current values that have been changed are not modified, but
/// any unchanged values are updated with the current database
/// values. No changes are lost in this merge.
/// </summary>
KeepChanges,
/// <summary>
/// All current values are overwritten with current database values,
/// regardless of whether they have been changed.
/// </summary>
OverwriteCurrentValues
}
/// <summary>
/// The DataContext is the source of all entities mapped over a database connection.
/// It tracks changes made to all retrieved entities and maintains an 'identity cache'
/// that guarantees that entities retrieved more than once are represented using the
/// same object instance.
/// </summary>
public class DataContext : IDisposable {
CommonDataServices services;
IProvider provider;
Dictionary<MetaTable, ITable> tables;
bool objectTrackingEnabled = true;
bool deferredLoadingEnabled = true;
bool disposed;
bool isInSubmitChanges;
DataLoadOptions loadOptions;
ChangeConflictCollection conflicts;
private DataContext() {
}
public DataContext(string fileOrServerOrConnection) {
if (fileOrServerOrConnection == null) {
throw Error.ArgumentNull("fileOrServerOrConnection");
}
this.InitWithDefaultMapping(fileOrServerOrConnection);
}
public DataContext(string fileOrServerOrConnection, MappingSource mapping) {
if (fileOrServerOrConnection == null) {
throw Error.ArgumentNull("fileOrServerOrConnection");
}
if (mapping == null) {
throw Error.ArgumentNull("mapping");
}
this.Init(fileOrServerOrConnection, mapping);
}
public DataContext(IDbConnection connection) {
if (connection == null) {
throw Error.ArgumentNull("connection");
}
this.InitWithDefaultMapping(connection);
}
public DataContext(IDbConnection connection, MappingSource mapping) {
if (connection == null) {
throw Error.ArgumentNull("connection");
}
if (mapping == null) {
throw Error.ArgumentNull("mapping");
}
this.Init(connection, mapping);
}
internal DataContext(DataContext context) {
if (context == null) {
throw Error.ArgumentNull("context");
}
this.Init(context.Connection, context.Mapping.MappingSource);
this.LoadOptions = context.LoadOptions;
this.Transaction = context.Transaction;
this.Log = context.Log;
this.CommandTimeout = context.CommandTimeout;
}
#region Dispose\Finalize
public void Dispose() {
this.disposed = true;
Dispose(true);
// Technically, calling GC.SuppressFinalize is not required because the class does not
// have a finalizer, but it does no harm, protects against the case where a finalizer is added
// in the future, and prevents an FxCop warning.
GC.SuppressFinalize(this);
}
// Not implementing finalizer here because there are no unmanaged resources
// to release. See http://msdnwiki.microsoft.com/en-us/mtpswiki/12afb1ea-3a17-4a3f-a1f0-fcdb853e2359.aspx
// The bulk of the clean-up code is implemented in Dispose(bool)
protected virtual void Dispose(bool disposing) {
// Implemented but empty so that derived contexts can implement
// a finalizer that potentially cleans up unmanaged resources.
if (disposing) {
if (this.provider != null) {
this.provider.Dispose();
this.provider = null;
}
this.services = null;
this.tables = null;
this.loadOptions = null;
}
}
internal void CheckDispose() {
if (this.disposed) {
throw Error.DataContextCannotBeUsedAfterDispose();
}
}
#endregion
private void InitWithDefaultMapping(object connection) {
this.Init(connection, new AttributeMappingSource());
}
internal object Clone() {
CheckDispose();
return Activator.CreateInstance(this.GetType(), new object[] { this.Connection, this.Mapping.MappingSource });
}
private void Init(object connection, MappingSource mapping) {
MetaModel model = mapping.GetModel(this.GetType());
this.services = new CommonDataServices(this, model);
this.conflicts = new ChangeConflictCollection();
// determine provider
Type providerType;
if (model.ProviderType != null) {
providerType = model.ProviderType;
}
else {
throw Error.ProviderTypeNull();
}
if (!typeof(IProvider).IsAssignableFrom(providerType)) {
throw Error.ProviderDoesNotImplementRequiredInterface(providerType, typeof(IProvider));
}
this.provider = (IProvider)Activator.CreateInstance(providerType);
this.provider.Initialize(this.services, connection);
this.tables = new Dictionary<MetaTable, ITable>();
this.InitTables(this);
}
internal void ClearCache() {
CheckDispose();
this.services.ResetServices();
}
internal CommonDataServices Services {
get {
CheckDispose();
return this.services;
}
}
/// <summary>
/// The connection object used by this DataContext when executing queries and commands.
/// </summary>
public DbConnection Connection {
get {
CheckDispose();
return this.provider.Connection;
}
}
/// <summary>
/// The transaction object used by this DataContext when executing queries and commands.
/// </summary>
public DbTransaction Transaction {
get {
CheckDispose();
return this.provider.Transaction;
}
set {
CheckDispose();
this.provider.Transaction = value;
}
}
/// <summary>
/// The command timeout to use when executing commands.
/// </summary>
public int CommandTimeout {
get {
CheckDispose();
return this.provider.CommandTimeout;
}
set {
CheckDispose();
this.provider.CommandTimeout = value;
}
}
/// <summary>
/// A text writer used by this DataContext to output information such as query and commands
/// being executed.
/// </summary>
public TextWriter Log {
get {
CheckDispose();
return this.provider.Log;
}
set {
CheckDispose();
this.provider.Log = value;
}
}
/// <summary>
/// True if object tracking is enabled, false otherwise. Object tracking
/// includes identity caching and change tracking. If tracking is turned off,
/// SubmitChanges and related functionality is disabled. DeferredLoading is
/// also disabled when object tracking is disabled.
/// </summary>
public bool ObjectTrackingEnabled {
get {
CheckDispose();
return objectTrackingEnabled;
}
set {
CheckDispose();
if (Services.HasCachedObjects) {
throw Error.OptionsCannotBeModifiedAfterQuery();
}
objectTrackingEnabled = value;
if (!objectTrackingEnabled) {
deferredLoadingEnabled = false;
}
// force reinitialization of cache/tracking objects
services.ResetServices();
}
}
/// <summary>
/// True if deferred loading is enabled, false otherwise. With deferred
/// loading disabled, association members return default values and are
/// not defer loaded.
/// </summary>
public bool DeferredLoadingEnabled {
get {
CheckDispose();
return deferredLoadingEnabled;
}
set {
CheckDispose();
if (Services.HasCachedObjects) {
throw Error.OptionsCannotBeModifiedAfterQuery();
}
// can't have tracking disabled and deferred loading enabled
if (!ObjectTrackingEnabled && value) {
throw Error.DeferredLoadingRequiresObjectTracking();
}
deferredLoadingEnabled = value;
}
}
/// <summary>
/// The mapping model used to describe the entities
/// </summary>
public MetaModel Mapping {
get {
CheckDispose();
return this.services.Model;
}
}
/// <summary>
/// Verify that change tracking is enabled, and throw an exception
/// if it is not.
/// </summary>
internal void VerifyTrackingEnabled() {
CheckDispose();
if (!ObjectTrackingEnabled) {
throw Error.ObjectTrackingRequired();
}
}
/// <summary>
/// Verify that submit changes is not occurring
/// </summary>
internal void CheckNotInSubmitChanges() {
CheckDispose();
if (this.isInSubmitChanges) {
throw Error.CannotPerformOperationDuringSubmitChanges();
}
}
/// <summary>
/// Verify that submit changes is occurring
/// </summary>
internal void CheckInSubmitChanges() {
CheckDispose();
if (!this.isInSubmitChanges) {
throw Error.CannotPerformOperationOutsideSubmitChanges();
}
}
/// <summary>
/// Returns the strongly-typed Table object representing a collection of persistent entities.
/// Use this collection as the starting point for queries.
/// </summary>
/// <typeparam name="TEntity">The type of the entity objects. In case of a persistent hierarchy
/// the entity specified must be the base type of the hierarchy.</typeparam>
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
public Table<TEntity> GetTable<TEntity>() where TEntity : class {
CheckDispose();
MetaTable metaTable = this.services.Model.GetTable(typeof(TEntity));
if (metaTable == null) {
throw Error.TypeIsNotMarkedAsTable(typeof(TEntity));
}
ITable table = this.GetTable(metaTable);
if (table.ElementType != typeof(TEntity)) {
throw Error.CouldNotGetTableForSubtype(typeof(TEntity), metaTable.RowType.Type);
}
return (Table<TEntity>)table;
}
/// <summary>
/// Returns the weakly-typed ITable object representing a collection of persistent entities.
/// Use this collection as the starting point for dynamic/runtime-computed queries.
/// </summary>
/// <param name="type">The type of the entity objects. In case of a persistent hierarchy
/// the entity specified must be the base type of the hierarchy.</param>
/// <returns></returns>
public ITable GetTable(Type type) {
CheckDispose();
if (type == null) {
throw Error.ArgumentNull("type");
}
MetaTable metaTable = this.services.Model.GetTable(type);
if (metaTable == null) {
throw Error.TypeIsNotMarkedAsTable(type);
}
if (metaTable.RowType.Type != type) {
throw Error.CouldNotGetTableForSubtype(type, metaTable.RowType.Type);
}
return this.GetTable(metaTable);
}
private ITable GetTable(MetaTable metaTable) {
System.Diagnostics.Debug.Assert(metaTable != null);
ITable tb;
if (!this.tables.TryGetValue(metaTable, out tb)) {
ValidateTable(metaTable);
Type tbType = typeof(Table<>).MakeGenericType(metaTable.RowType.Type);
tb = (ITable)Activator.CreateInstance(tbType, BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic, null, new object[] { this, metaTable }, null);
this.tables.Add(metaTable, tb);
}
return tb;
}
private static void ValidateTable(MetaTable metaTable) {
// Associations can only be between entities - verify both that both ends of all
// associations are entities.
foreach(MetaAssociation assoc in metaTable.RowType.Associations) {
if(!assoc.ThisMember.DeclaringType.IsEntity) {
throw Error.NonEntityAssociationMapping(assoc.ThisMember.DeclaringType.Type, assoc.ThisMember.Name, assoc.ThisMember.DeclaringType.Type);
}
if(!assoc.OtherType.IsEntity) {
throw Error.NonEntityAssociationMapping(assoc.ThisMember.DeclaringType.Type, assoc.ThisMember.Name, assoc.OtherType.Type);
}
}
}
private void InitTables(object schema) {
FieldInfo[] fields = schema.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in fields) {
Type ft = fi.FieldType;
if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(Table<>)) {
ITable tb = (ITable)fi.GetValue(schema);
if (tb == null) {
Type rowType = ft.GetGenericArguments()[0];
tb = this.GetTable(rowType);
fi.SetValue(schema, tb);
}
}
}
}
/// <summary>
/// Internal method that can be accessed by tests to retrieve the provider
/// The IProvider result can then be cast to the actual provider to call debug methods like
/// CheckQueries, QueryCount, EnableCacheLookup
/// </summary>
internal IProvider Provider {
get {
CheckDispose();
return this.provider;
}
}
/// <summary>
/// Returns true if the database specified by the connection object exists.
/// </summary>
/// <returns></returns>
public bool DatabaseExists() {
CheckDispose();
return this.provider.DatabaseExists();
}
/// <summary>
/// Creates a new database instance (catalog or file) at the location specified by the connection
/// using the metadata encoded within the entities or mapping file.
/// </summary>
public void CreateDatabase() {
CheckDispose();
this.provider.CreateDatabase();
}
/// <summary>
/// Deletes the database instance at the location specified by the connection.
/// </summary>
public void DeleteDatabase() {
CheckDispose();
this.provider.DeleteDatabase();
}
/// <summary>
/// Submits one or more commands to the database reflecting the changes made to the retreived entities.
/// If a transaction is not already specified one will be created for the duration of this operation.
/// If a change conflict is encountered a ChangeConflictException will be thrown.
/// </summary>
public void SubmitChanges() {
CheckDispose();
SubmitChanges(ConflictMode.FailOnFirstConflict);
}
/// <summary>
/// Submits one or more commands to the database reflecting the changes made to the retreived entities.
/// If a transaction is not already specified one will be created for the duration of this operation.
/// If a change conflict is encountered a ChangeConflictException will be thrown.
/// You can override this method to implement common conflict resolution behaviors.
/// </summary>
/// <param name="failureMode">Determines how SubmitChanges handles conflicts.</param>
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Microsoft: In the middle of attempting to rollback a transaction, outer transaction is thrown.")]
public virtual void SubmitChanges(ConflictMode failureMode) {
CheckDispose();
CheckNotInSubmitChanges();
VerifyTrackingEnabled();
this.conflicts.Clear();
try {
this.isInSubmitChanges = true;
if (System.Transactions.Transaction.Current == null && this.provider.Transaction == null) {
bool openedConnection = false;
DbTransaction transaction = null;
try {
if (this.provider.Connection.State == ConnectionState.Open) {
this.provider.ClearConnection();
}
if (this.provider.Connection.State == ConnectionState.Closed) {
this.provider.Connection.Open();
openedConnection = true;
}
transaction = this.provider.Connection.BeginTransaction(IsolationLevel.ReadCommitted);
this.provider.Transaction = transaction;
new ChangeProcessor(this.services, this).SubmitChanges(failureMode);
this.AcceptChanges();
// to commit a transaction, there can be no open readers
// on the connection.
this.provider.ClearConnection();
transaction.Commit();
}
catch {
if (transaction != null) {
transaction.Rollback();
}
throw;
}
finally {
this.provider.Transaction = null;
if (openedConnection) {
this.provider.Connection.Close();
}
}
}
else {
new ChangeProcessor(services, this).SubmitChanges(failureMode);
this.AcceptChanges();
}
}
finally {
this.isInSubmitChanges = false;
}
}
/// <summary>
/// Refresh the specified object using the mode specified. If the refresh
/// cannot be performed (for example if the object no longer exists in the
/// database) an InvalidOperationException is thrown.
/// </summary>
/// <param name="mode">How the refresh should be performed.</param>
/// <param name="entity">The object to refresh. The object must be
/// the result of a previous query.</param>
public void Refresh(RefreshMode mode, object entity)
{
CheckDispose();
CheckNotInSubmitChanges();
VerifyTrackingEnabled();
if (entity == null)
{
throw Error.ArgumentNull("entity");
}
Array items = Array.CreateInstance(entity.GetType(), 1);
items.SetValue(entity, 0);
this.Refresh(mode, items as IEnumerable);
}
/// <summary>
/// Refresh a set of objects using the mode specified. If the refresh
/// cannot be performed (for example if the object no longer exists in the
/// database) an InvalidOperationException is thrown.
/// </summary>
/// <param name="mode">How the refresh should be performed.</param>
/// <param name="entities">The objects to refresh.</param>
public void Refresh(RefreshMode mode, params object[] entities)
{
CheckDispose(); // code hygeine requirement
if (entities == null){
throw Error.ArgumentNull("entities");
}
Refresh(mode, (IEnumerable)entities);
}
/// <summary>
/// Refresh a collection of objects using the mode specified. If the refresh
/// cannot be performed (for example if the object no longer exists in the
/// database) an InvalidOperationException is thrown.
/// </summary>
/// <param name="mode">How the refresh should be performed.</param>
/// <param name="entities">The collection of objects to refresh.</param>
public void Refresh(RefreshMode mode, IEnumerable entities)
{
CheckDispose();
CheckNotInSubmitChanges();
VerifyTrackingEnabled();
if (entities == null) {
throw Error.ArgumentNull("entities");
}
// if the collection is a query, we need to execute and buffer,
// since below we will be issuing additional queries and can only
// have a single reader open.
var list = entities.Cast<object>().ToList();
// create a fresh context to fetch new state from
DataContext refreshContext = this.CreateRefreshContext();
foreach (object o in list) {
// verify that each object in the list is an entity
MetaType inheritanceRoot = services.Model.GetMetaType(o.GetType()).InheritanceRoot;
GetTable(inheritanceRoot.Type);
TrackedObject trackedObject = this.services.ChangeTracker.GetTrackedObject(o);
if (trackedObject == null) {
throw Error.UnrecognizedRefreshObject();
}
if (trackedObject.IsNew) {
throw Error.RefreshOfNewObject();
}
// query to get the current database values
object[] keyValues = CommonDataServices.GetKeyValues(trackedObject.Type, trackedObject.Original);
object freshInstance = refreshContext.Services.GetObjectByKey(trackedObject.Type, keyValues);
if (freshInstance == null) {
throw Error.RefreshOfDeletedObject();
}
// refresh the tracked object using the new values and
// the mode specified.
trackedObject.Refresh(mode, freshInstance);
}
}
internal DataContext CreateRefreshContext() {
CheckDispose();
return new DataContext(this);
}
private void AcceptChanges() {
CheckDispose();
VerifyTrackingEnabled();
this.services.ChangeTracker.AcceptChanges();
}
/// <summary>
/// Returns the query text in the database server's native query language
/// that would need to be executed to perform the specified query.
/// </summary>
/// <param name="query">The query</param>
/// <returns></returns>
internal string GetQueryText(IQueryable query) {
CheckDispose();
if (query == null) {
throw Error.ArgumentNull("query");
}
return this.provider.GetQueryText(query.Expression);
}
/// <summary>
/// Returns an IDbCommand object representing the query in the database server's
/// native query language.
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public DbCommand GetCommand(IQueryable query) {
CheckDispose();
if (query == null) {
throw Error.ArgumentNull("query");
}
return this.provider.GetCommand(query.Expression);
}
/// <summary>
/// Returns the command text in the database server's native query langauge
/// that would need to be executed in order to persist the changes made to the
/// objects back into the database.
/// </summary>
/// <returns></returns>
internal string GetChangeText() {
CheckDispose();
VerifyTrackingEnabled();
return new ChangeProcessor(services, this).GetChangeText();
}
/// <summary>
/// Computes the un-ordered set of objects that have changed
/// </summary>
/// <returns></returns>
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "ChangeSet", Justification="The capitalization was deliberately chosen.")]
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Non-trivial operations are not suitable for properties.")]
public ChangeSet GetChangeSet() {
CheckDispose();
return new ChangeProcessor(this.services, this).GetChangeSet();
}
/// <summary>
/// Execute a command against the database server that does not return a sequence of objects.
/// The command is specified using the server's native query language, such as SQL.
/// </summary>
/// <param name="command">The command specified in the server's native query language.</param>
/// <param name="parameters">The parameter values to use for the query.</param>
/// <returns>A single integer return value</returns>
public int ExecuteCommand(string command, params object[] parameters) {
CheckDispose();
if (command == null) {
throw Error.ArgumentNull("command");
}
if (parameters == null) {
throw Error.ArgumentNull("parameters");
}
return (int)this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(), command, parameters).ReturnValue;
}
/// <summary>
/// Execute the sequence returning query against the database server.
/// The query is specified using the server's native query language, such as SQL.
/// </summary>
/// <typeparam name="TResult">The element type of the result sequence.</typeparam>
/// <param name="query">The query specified in the server's native query language.</param>
/// <param name="parameters">The parameter values to use for the query.</param>
/// <returns>An IEnumerable sequence of objects.</returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
public IEnumerable<TResult> ExecuteQuery<TResult>(string query, params object[] parameters) {
CheckDispose();
if (query == null) {
throw Error.ArgumentNull("query");
}
if (parameters == null) {
throw Error.ArgumentNull("parameters");
}
return (IEnumerable<TResult>)this.ExecuteMethodCall(this, ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)), query, parameters).ReturnValue;
}
/// <summary>
/// Execute the sequence returning query against the database server.
/// The query is specified using the server's native query language, such as SQL.
/// </summary>
/// <param name="elementType">The element type of the result sequence.</param>
/// <param name="query">The query specified in the server's native query language.</param>
/// <param name="parameters">The parameter values to use for the query.</param>
/// <returns></returns>
public IEnumerable ExecuteQuery(Type elementType, string query, params object[] parameters) {
CheckDispose();
if (elementType == null) {
throw Error.ArgumentNull("elementType");
}
if (query == null) {
throw Error.ArgumentNull("query");
}
if (parameters == null) {
throw Error.ArgumentNull("parameters");
}
if (_miExecuteQuery == null) {
_miExecuteQuery = typeof(DataContext).GetMethods().Single(m => m.Name == "ExecuteQuery" && m.GetParameters().Length == 2);
}
return (IEnumerable)this.ExecuteMethodCall(this, _miExecuteQuery.MakeGenericMethod(elementType), query, parameters).ReturnValue;
}
private static MethodInfo _miExecuteQuery;
/// <summary>
/// Executes the equivalent of the specified method call on the database server.
/// </summary>
/// <param name="instance">The instance the method is being called on.</param>
/// <param name="methodInfo">The reflection MethodInfo for the method to invoke.</param>
/// <param name="parameters">The parameters for the method call.</param>
/// <returns>The result of the method call. Use this type's ReturnValue property to access the actual return value.</returns>
internal protected IExecuteResult ExecuteMethodCall(object instance, MethodInfo methodInfo, params object[] parameters) {
CheckDispose();
if (instance == null) {
throw Error.ArgumentNull("instance");
}
if (methodInfo == null) {
throw Error.ArgumentNull("methodInfo");
}
if (parameters == null) {
throw Error.ArgumentNull("parameters");
}
return this.provider.Execute(this.GetMethodCall(instance, methodInfo, parameters));
}
/// <summary>
/// Create a query object for the specified method call.
/// </summary>
/// <typeparam name="TResult">The element type of the query.</typeparam>
/// <param name="instance">The instance the method is being called on.</param>
/// <param name="methodInfo">The reflection MethodInfo for the method to invoke.</param>
/// <param name="parameters">The parameters for the method call.</param>
/// <returns>The returned query object</returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
internal protected IQueryable<TResult> CreateMethodCallQuery<TResult>(object instance, MethodInfo methodInfo, params object[] parameters) {
CheckDispose();
if (instance == null) {
throw Error.ArgumentNull("instance");
}
if (methodInfo == null) {
throw Error.ArgumentNull("methodInfo");
}
if (parameters == null) {
throw Error.ArgumentNull("parameters");
}
if (!typeof(IQueryable<TResult>).IsAssignableFrom(methodInfo.ReturnType)) {
throw Error.ExpectedQueryableArgument("methodInfo", typeof(IQueryable<TResult>));
}
return new DataQuery<TResult>(this, this.GetMethodCall(instance, methodInfo, parameters));
}
private Expression GetMethodCall(object instance, MethodInfo methodInfo, params object[] parameters) {
CheckDispose();
if (parameters.Length > 0) {
ParameterInfo[] pis = methodInfo.GetParameters();
List<Expression> args = new List<Expression>(parameters.Length);
for (int i = 0, n = parameters.Length; i < n; i++) {
Type pType = pis[i].ParameterType;
if (pType.IsByRef) {
pType = pType.GetElementType();
}
args.Add(Expression.Constant(parameters[i], pType));
}
return Expression.Call(Expression.Constant(instance), methodInfo, args);
}
return Expression.Call(Expression.Constant(instance), methodInfo);
}
/// <summary>
/// Execute a dynamic insert
/// </summary>
/// <param name="entity"></param>
internal protected void ExecuteDynamicInsert(object entity) {
CheckDispose();
if (entity == null) {
throw Error.ArgumentNull("entity");
}
this.CheckInSubmitChanges();
TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
if (tracked == null) {
throw Error.CannotPerformOperationForUntrackedObject();
}
this.services.ChangeDirector.DynamicInsert(tracked);
}
/// <summary>
/// Execute a dynamic update
/// </summary>
/// <param name="entity"></param>
internal protected void ExecuteDynamicUpdate(object entity) {
CheckDispose();
if (entity == null) {
throw Error.ArgumentNull("entity");
}
this.CheckInSubmitChanges();
TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
if (tracked == null) {
throw Error.CannotPerformOperationForUntrackedObject();
}
int result = this.services.ChangeDirector.DynamicUpdate(tracked);
if (result == 0) {
throw new ChangeConflictException();
}
}
/// <summary>
/// Execute a dynamic delete
/// </summary>
/// <param name="entity"></param>
internal protected void ExecuteDynamicDelete(object entity) {
CheckDispose();
if (entity == null) {
throw Error.ArgumentNull("entity");
}
this.CheckInSubmitChanges();
TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
if (tracked == null) {
throw Error.CannotPerformOperationForUntrackedObject();
}
int result = this.services.ChangeDirector.DynamicDelete(tracked);
if (result == 0) {
throw new ChangeConflictException();
}
}
/// <summary>
/// Translates the data from a DbDataReader into sequence of objects.
/// </summary>
/// <typeparam name="TResult">The element type of the resulting sequence</typeparam>
/// <param name="reader">The DbDataReader to translate</param>
/// <returns>The translated sequence of objects</returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Microsoft: Generic parameters are required for strong-typing of the return type.")]
public IEnumerable<TResult> Translate<TResult>(DbDataReader reader) {
CheckDispose();
return (IEnumerable<TResult>)this.Translate(typeof(TResult), reader);
}
/// <summary>
/// Translates the data from a DbDataReader into sequence of objects.
/// </summary>
/// <param name="elementType">The element type of the resulting sequence</param>
/// <param name="reader">The DbDataReader to translate</param>
/// <returns>The translated sequence of objects</returns>
public IEnumerable Translate(Type elementType, DbDataReader reader) {
CheckDispose();
if (elementType == null) {
throw Error.ArgumentNull("elementType");
}
if (reader == null) {
throw Error.ArgumentNull("reader");
}
return this.provider.Translate(elementType, reader);
}
/// <summary>
/// Translates the data from a DbDataReader into IMultipleResults.
/// </summary>
/// <param name="reader">The DbDataReader to translate</param>
/// <returns>The translated sequence of objects</returns>
public IMultipleResults Translate(DbDataReader reader) {
CheckDispose();
if (reader == null) {
throw Error.ArgumentNull("reader");
}
return this.provider.Translate(reader);
}
/// <summary>
/// Remove all Include\Subquery LoadOptions settings.
/// </summary>
internal void ResetLoadOptions() {
CheckDispose();
this.loadOptions = null;
}
/// <summary>
/// The DataLoadOptions used to define prefetch behavior for defer loaded members
/// and membership of related collections.
/// </summary>
public DataLoadOptions LoadOptions {
get {
CheckDispose();
return this.loadOptions;
}
set {
CheckDispose();
if (this.services.HasCachedObjects && value != this.loadOptions) {
throw Error.LoadOptionsChangeNotAllowedAfterQuery();
}
if (value != null) {
value.Freeze();
}
this.loadOptions = value;
}
}
/// <summary>
/// This list of change conflicts produced by the last call to SubmitChanges. Use this collection
/// to resolve conflicts after catching a ChangeConflictException and before calling SubmitChanges again.
/// </summary>
public ChangeConflictCollection ChangeConflicts {
get {
CheckDispose();
return this.conflicts;
}
}
}
/// <summary>
/// Defines behavior for implementations of IQueryable that allow modifications to the membership of the resulting set.
/// </summary>
/// <typeparam name="TEntity">Type of entities returned from the queryable.</typeparam>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
public interface ITable<TEntity> : IQueryable<TEntity>
where TEntity : class
{
/// <summary>
/// Notify the set that an object representing a new entity should be added to the set.
/// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
/// until changes to that set have been persisted in some manner.
/// </summary>
/// <param name="entity">Entity object to be added.</param>
void InsertOnSubmit(TEntity entity);
/// <summary>
/// Notify the set that an object representing a new entity should be added to the set.
/// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
/// until changes to that set have been persisted in some manner.
/// </summary>
/// <param name="entity">Entity object to be attached.</param>
void Attach(TEntity entity);
/// <summary>
/// Notify the set that an object representing an entity should be removed from the set.
/// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
/// until changes to that set have been persisted in some manner.
/// </summary>
/// <param name="entity">Entity object to be removed.</param>
/// <exception cref="InvalidOperationException">Throws if the specified object is not in the set.</exception>
void DeleteOnSubmit(TEntity entity);
}
/// <summary>
/// ITable is the common interface for DataContext tables. It can be used as the source
/// of a dynamic/runtime-generated query.