DataContext.cs 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Data.Common;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Reflection;
  14. using System.Text;
  15. using System.Transactions;
  16. using System.Xml;
  17. using System.Runtime.CompilerServices;
  18. namespace System.Data.Linq {
  19. using System.Data.Linq.Mapping;
  20. using System.Data.Linq.Provider;
  21. using System.Diagnostics.CodeAnalysis;
  22. /// <summary>
  23. /// Used to specify how a submit should behave when one
  24. /// or more updates fail due to optimistic concurrency
  25. /// conflicts.
  26. /// </summary>
  27. public enum ConflictMode {
  28. /// <summary>
  29. /// Fail immediately when the first change conflict is encountered.
  30. /// </summary>
  31. FailOnFirstConflict,
  32. /// <summary>
  33. /// Only fail after all changes have been attempted.
  34. /// </summary>
  35. ContinueOnConflict
  36. }
  37. /// <summary>
  38. /// Used to specify a value synchronization strategy.
  39. /// </summary>
  40. public enum RefreshMode {
  41. /// <summary>
  42. /// Keep the current values.
  43. /// </summary>
  44. KeepCurrentValues,
  45. /// <summary>
  46. /// Current values that have been changed are not modified, but
  47. /// any unchanged values are updated with the current database
  48. /// values. No changes are lost in this merge.
  49. /// </summary>
  50. KeepChanges,
  51. /// <summary>
  52. /// All current values are overwritten with current database values,
  53. /// regardless of whether they have been changed.
  54. /// </summary>
  55. OverwriteCurrentValues
  56. }
  57. /// <summary>
  58. /// The DataContext is the source of all entities mapped over a database connection.
  59. /// It tracks changes made to all retrieved entities and maintains an 'identity cache'
  60. /// that guarantees that entities retrieved more than once are represented using the
  61. /// same object instance.
  62. /// </summary>
  63. public class DataContext : IDisposable {
  64. CommonDataServices services;
  65. IProvider provider;
  66. Dictionary<MetaTable, ITable> tables;
  67. bool objectTrackingEnabled = true;
  68. bool deferredLoadingEnabled = true;
  69. bool disposed;
  70. bool isInSubmitChanges;
  71. DataLoadOptions loadOptions;
  72. ChangeConflictCollection conflicts;
  73. private DataContext() {
  74. }
  75. public DataContext(string fileOrServerOrConnection) {
  76. if (fileOrServerOrConnection == null) {
  77. throw Error.ArgumentNull("fileOrServerOrConnection");
  78. }
  79. this.InitWithDefaultMapping(fileOrServerOrConnection);
  80. }
  81. public DataContext(string fileOrServerOrConnection, MappingSource mapping) {
  82. if (fileOrServerOrConnection == null) {
  83. throw Error.ArgumentNull("fileOrServerOrConnection");
  84. }
  85. if (mapping == null) {
  86. throw Error.ArgumentNull("mapping");
  87. }
  88. this.Init(fileOrServerOrConnection, mapping);
  89. }
  90. public DataContext(IDbConnection connection) {
  91. if (connection == null) {
  92. throw Error.ArgumentNull("connection");
  93. }
  94. this.InitWithDefaultMapping(connection);
  95. }
  96. public DataContext(IDbConnection connection, MappingSource mapping) {
  97. if (connection == null) {
  98. throw Error.ArgumentNull("connection");
  99. }
  100. if (mapping == null) {
  101. throw Error.ArgumentNull("mapping");
  102. }
  103. this.Init(connection, mapping);
  104. }
  105. internal DataContext(DataContext context) {
  106. if (context == null) {
  107. throw Error.ArgumentNull("context");
  108. }
  109. this.Init(context.Connection, context.Mapping.MappingSource);
  110. this.LoadOptions = context.LoadOptions;
  111. this.Transaction = context.Transaction;
  112. this.Log = context.Log;
  113. this.CommandTimeout = context.CommandTimeout;
  114. }
  115. #region Dispose\Finalize
  116. public void Dispose() {
  117. this.disposed = true;
  118. Dispose(true);
  119. // Technically, calling GC.SuppressFinalize is not required because the class does not
  120. // have a finalizer, but it does no harm, protects against the case where a finalizer is added
  121. // in the future, and prevents an FxCop warning.
  122. GC.SuppressFinalize(this);
  123. }
  124. // Not implementing finalizer here because there are no unmanaged resources
  125. // to release. See http://msdnwiki.microsoft.com/en-us/mtpswiki/12afb1ea-3a17-4a3f-a1f0-fcdb853e2359.aspx
  126. // The bulk of the clean-up code is implemented in Dispose(bool)
  127. protected virtual void Dispose(bool disposing) {
  128. // Implemented but empty so that derived contexts can implement
  129. // a finalizer that potentially cleans up unmanaged resources.
  130. if (disposing) {
  131. if (this.provider != null) {
  132. this.provider.Dispose();
  133. this.provider = null;
  134. }
  135. this.services = null;
  136. this.tables = null;
  137. this.loadOptions = null;
  138. }
  139. }
  140. internal void CheckDispose() {
  141. if (this.disposed) {
  142. throw Error.DataContextCannotBeUsedAfterDispose();
  143. }
  144. }
  145. #endregion
  146. private void InitWithDefaultMapping(object connection) {
  147. this.Init(connection, new AttributeMappingSource());
  148. }
  149. internal object Clone() {
  150. CheckDispose();
  151. return Activator.CreateInstance(this.GetType(), new object[] { this.Connection, this.Mapping.MappingSource });
  152. }
  153. private void Init(object connection, MappingSource mapping) {
  154. MetaModel model = mapping.GetModel(this.GetType());
  155. this.services = new CommonDataServices(this, model);
  156. this.conflicts = new ChangeConflictCollection();
  157. // determine provider
  158. Type providerType;
  159. if (model.ProviderType != null) {
  160. providerType = model.ProviderType;
  161. }
  162. else {
  163. throw Error.ProviderTypeNull();
  164. }
  165. if (!typeof(IProvider).IsAssignableFrom(providerType)) {
  166. throw Error.ProviderDoesNotImplementRequiredInterface(providerType, typeof(IProvider));
  167. }
  168. this.provider = (IProvider)Activator.CreateInstance(providerType);
  169. this.provider.Initialize(this.services, connection);
  170. this.tables = new Dictionary<MetaTable, ITable>();
  171. this.InitTables(this);
  172. }
  173. internal void ClearCache() {
  174. CheckDispose();
  175. this.services.ResetServices();
  176. }
  177. internal CommonDataServices Services {
  178. get {
  179. CheckDispose();
  180. return this.services;
  181. }
  182. }
  183. /// <summary>
  184. /// The connection object used by this DataContext when executing queries and commands.
  185. /// </summary>
  186. public DbConnection Connection {
  187. get {
  188. CheckDispose();
  189. return this.provider.Connection;
  190. }
  191. }
  192. /// <summary>
  193. /// The transaction object used by this DataContext when executing queries and commands.
  194. /// </summary>
  195. public DbTransaction Transaction {
  196. get {
  197. CheckDispose();
  198. return this.provider.Transaction;
  199. }
  200. set {
  201. CheckDispose();
  202. this.provider.Transaction = value;
  203. }
  204. }
  205. /// <summary>
  206. /// The command timeout to use when executing commands.
  207. /// </summary>
  208. public int CommandTimeout {
  209. get {
  210. CheckDispose();
  211. return this.provider.CommandTimeout;
  212. }
  213. set {
  214. CheckDispose();
  215. this.provider.CommandTimeout = value;
  216. }
  217. }
  218. /// <summary>
  219. /// A text writer used by this DataContext to output information such as query and commands
  220. /// being executed.
  221. /// </summary>
  222. public TextWriter Log {
  223. get {
  224. CheckDispose();
  225. return this.provider.Log;
  226. }
  227. set {
  228. CheckDispose();
  229. this.provider.Log = value;
  230. }
  231. }
  232. /// <summary>
  233. /// True if object tracking is enabled, false otherwise. Object tracking
  234. /// includes identity caching and change tracking. If tracking is turned off,
  235. /// SubmitChanges and related functionality is disabled. DeferredLoading is
  236. /// also disabled when object tracking is disabled.
  237. /// </summary>
  238. public bool ObjectTrackingEnabled {
  239. get {
  240. CheckDispose();
  241. return objectTrackingEnabled;
  242. }
  243. set {
  244. CheckDispose();
  245. if (Services.HasCachedObjects) {
  246. throw Error.OptionsCannotBeModifiedAfterQuery();
  247. }
  248. objectTrackingEnabled = value;
  249. if (!objectTrackingEnabled) {
  250. deferredLoadingEnabled = false;
  251. }
  252. // force reinitialization of cache/tracking objects
  253. services.ResetServices();
  254. }
  255. }
  256. /// <summary>
  257. /// True if deferred loading is enabled, false otherwise. With deferred
  258. /// loading disabled, association members return default values and are
  259. /// not defer loaded.
  260. /// </summary>
  261. public bool DeferredLoadingEnabled {
  262. get {
  263. CheckDispose();
  264. return deferredLoadingEnabled;
  265. }
  266. set {
  267. CheckDispose();
  268. if (Services.HasCachedObjects) {
  269. throw Error.OptionsCannotBeModifiedAfterQuery();
  270. }
  271. // can't have tracking disabled and deferred loading enabled
  272. if (!ObjectTrackingEnabled && value) {
  273. throw Error.DeferredLoadingRequiresObjectTracking();
  274. }
  275. deferredLoadingEnabled = value;
  276. }
  277. }
  278. /// <summary>
  279. /// The mapping model used to describe the entities
  280. /// </summary>
  281. public MetaModel Mapping {
  282. get {
  283. CheckDispose();
  284. return this.services.Model;
  285. }
  286. }
  287. /// <summary>
  288. /// Verify that change tracking is enabled, and throw an exception
  289. /// if it is not.
  290. /// </summary>
  291. internal void VerifyTrackingEnabled() {
  292. CheckDispose();
  293. if (!ObjectTrackingEnabled) {
  294. throw Error.ObjectTrackingRequired();
  295. }
  296. }
  297. /// <summary>
  298. /// Verify that submit changes is not occurring
  299. /// </summary>
  300. internal void CheckNotInSubmitChanges() {
  301. CheckDispose();
  302. if (this.isInSubmitChanges) {
  303. throw Error.CannotPerformOperationDuringSubmitChanges();
  304. }
  305. }
  306. /// <summary>
  307. /// Verify that submit changes is occurring
  308. /// </summary>
  309. internal void CheckInSubmitChanges() {
  310. CheckDispose();
  311. if (!this.isInSubmitChanges) {
  312. throw Error.CannotPerformOperationOutsideSubmitChanges();
  313. }
  314. }
  315. /// <summary>
  316. /// Returns the strongly-typed Table object representing a collection of persistent entities.
  317. /// Use this collection as the starting point for queries.
  318. /// </summary>
  319. /// <typeparam name="TEntity">The type of the entity objects. In case of a persistent hierarchy
  320. /// the entity specified must be the base type of the hierarchy.</typeparam>
  321. /// <returns></returns>
  322. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  323. public Table<TEntity> GetTable<TEntity>() where TEntity : class {
  324. CheckDispose();
  325. MetaTable metaTable = this.services.Model.GetTable(typeof(TEntity));
  326. if (metaTable == null) {
  327. throw Error.TypeIsNotMarkedAsTable(typeof(TEntity));
  328. }
  329. ITable table = this.GetTable(metaTable);
  330. if (table.ElementType != typeof(TEntity)) {
  331. throw Error.CouldNotGetTableForSubtype(typeof(TEntity), metaTable.RowType.Type);
  332. }
  333. return (Table<TEntity>)table;
  334. }
  335. /// <summary>
  336. /// Returns the weakly-typed ITable object representing a collection of persistent entities.
  337. /// Use this collection as the starting point for dynamic/runtime-computed queries.
  338. /// </summary>
  339. /// <param name="type">The type of the entity objects. In case of a persistent hierarchy
  340. /// the entity specified must be the base type of the hierarchy.</param>
  341. /// <returns></returns>
  342. public ITable GetTable(Type type) {
  343. CheckDispose();
  344. if (type == null) {
  345. throw Error.ArgumentNull("type");
  346. }
  347. MetaTable metaTable = this.services.Model.GetTable(type);
  348. if (metaTable == null) {
  349. throw Error.TypeIsNotMarkedAsTable(type);
  350. }
  351. if (metaTable.RowType.Type != type) {
  352. throw Error.CouldNotGetTableForSubtype(type, metaTable.RowType.Type);
  353. }
  354. return this.GetTable(metaTable);
  355. }
  356. private ITable GetTable(MetaTable metaTable) {
  357. System.Diagnostics.Debug.Assert(metaTable != null);
  358. ITable tb;
  359. if (!this.tables.TryGetValue(metaTable, out tb)) {
  360. ValidateTable(metaTable);
  361. Type tbType = typeof(Table<>).MakeGenericType(metaTable.RowType.Type);
  362. tb = (ITable)Activator.CreateInstance(tbType, BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic, null, new object[] { this, metaTable }, null);
  363. this.tables.Add(metaTable, tb);
  364. }
  365. return tb;
  366. }
  367. private static void ValidateTable(MetaTable metaTable) {
  368. // Associations can only be between entities - verify both that both ends of all
  369. // associations are entities.
  370. foreach(MetaAssociation assoc in metaTable.RowType.Associations) {
  371. if(!assoc.ThisMember.DeclaringType.IsEntity) {
  372. throw Error.NonEntityAssociationMapping(assoc.ThisMember.DeclaringType.Type, assoc.ThisMember.Name, assoc.ThisMember.DeclaringType.Type);
  373. }
  374. if(!assoc.OtherType.IsEntity) {
  375. throw Error.NonEntityAssociationMapping(assoc.ThisMember.DeclaringType.Type, assoc.ThisMember.Name, assoc.OtherType.Type);
  376. }
  377. }
  378. }
  379. private void InitTables(object schema) {
  380. FieldInfo[] fields = schema.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
  381. foreach (FieldInfo fi in fields) {
  382. Type ft = fi.FieldType;
  383. if (ft.IsGenericType && ft.GetGenericTypeDefinition() == typeof(Table<>)) {
  384. ITable tb = (ITable)fi.GetValue(schema);
  385. if (tb == null) {
  386. Type rowType = ft.GetGenericArguments()[0];
  387. tb = this.GetTable(rowType);
  388. fi.SetValue(schema, tb);
  389. }
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// Internal method that can be accessed by tests to retrieve the provider
  395. /// The IProvider result can then be cast to the actual provider to call debug methods like
  396. /// CheckQueries, QueryCount, EnableCacheLookup
  397. /// </summary>
  398. internal IProvider Provider {
  399. get {
  400. CheckDispose();
  401. return this.provider;
  402. }
  403. }
  404. /// <summary>
  405. /// Returns true if the database specified by the connection object exists.
  406. /// </summary>
  407. /// <returns></returns>
  408. public bool DatabaseExists() {
  409. CheckDispose();
  410. return this.provider.DatabaseExists();
  411. }
  412. /// <summary>
  413. /// Creates a new database instance (catalog or file) at the location specified by the connection
  414. /// using the metadata encoded within the entities or mapping file.
  415. /// </summary>
  416. public void CreateDatabase() {
  417. CheckDispose();
  418. this.provider.CreateDatabase();
  419. }
  420. /// <summary>
  421. /// Deletes the database instance at the location specified by the connection.
  422. /// </summary>
  423. public void DeleteDatabase() {
  424. CheckDispose();
  425. this.provider.DeleteDatabase();
  426. }
  427. /// <summary>
  428. /// Submits one or more commands to the database reflecting the changes made to the retreived entities.
  429. /// If a transaction is not already specified one will be created for the duration of this operation.
  430. /// If a change conflict is encountered a ChangeConflictException will be thrown.
  431. /// </summary>
  432. public void SubmitChanges() {
  433. CheckDispose();
  434. SubmitChanges(ConflictMode.FailOnFirstConflict);
  435. }
  436. /// <summary>
  437. /// Submits one or more commands to the database reflecting the changes made to the retreived entities.
  438. /// If a transaction is not already specified one will be created for the duration of this operation.
  439. /// If a change conflict is encountered a ChangeConflictException will be thrown.
  440. /// You can override this method to implement common conflict resolution behaviors.
  441. /// </summary>
  442. /// <param name="failureMode">Determines how SubmitChanges handles conflicts.</param>
  443. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "[....]: In the middle of attempting to rollback a transaction, outer transaction is thrown.")]
  444. public virtual void SubmitChanges(ConflictMode failureMode) {
  445. CheckDispose();
  446. CheckNotInSubmitChanges();
  447. VerifyTrackingEnabled();
  448. this.conflicts.Clear();
  449. try {
  450. this.isInSubmitChanges = true;
  451. if (System.Transactions.Transaction.Current == null && this.provider.Transaction == null) {
  452. bool openedConnection = false;
  453. DbTransaction transaction = null;
  454. try {
  455. if (this.provider.Connection.State == ConnectionState.Open) {
  456. this.provider.ClearConnection();
  457. }
  458. if (this.provider.Connection.State == ConnectionState.Closed) {
  459. this.provider.Connection.Open();
  460. openedConnection = true;
  461. }
  462. transaction = this.provider.Connection.BeginTransaction(IsolationLevel.ReadCommitted);
  463. this.provider.Transaction = transaction;
  464. new ChangeProcessor(this.services, this).SubmitChanges(failureMode);
  465. this.AcceptChanges();
  466. // to commit a transaction, there can be no open readers
  467. // on the connection.
  468. this.provider.ClearConnection();
  469. transaction.Commit();
  470. }
  471. catch {
  472. if (transaction != null) {
  473. transaction.Rollback();
  474. }
  475. throw;
  476. }
  477. finally {
  478. this.provider.Transaction = null;
  479. if (openedConnection) {
  480. this.provider.Connection.Close();
  481. }
  482. }
  483. }
  484. else {
  485. new ChangeProcessor(services, this).SubmitChanges(failureMode);
  486. this.AcceptChanges();
  487. }
  488. }
  489. finally {
  490. this.isInSubmitChanges = false;
  491. }
  492. }
  493. /// <summary>
  494. /// Refresh the specified object using the mode specified. If the refresh
  495. /// cannot be performed (for example if the object no longer exists in the
  496. /// database) an InvalidOperationException is thrown.
  497. /// </summary>
  498. /// <param name="mode">How the refresh should be performed.</param>
  499. /// <param name="entity">The object to refresh. The object must be
  500. /// the result of a previous query.</param>
  501. public void Refresh(RefreshMode mode, object entity)
  502. {
  503. CheckDispose();
  504. CheckNotInSubmitChanges();
  505. VerifyTrackingEnabled();
  506. if (entity == null)
  507. {
  508. throw Error.ArgumentNull("entity");
  509. }
  510. Array items = Array.CreateInstance(entity.GetType(), 1);
  511. items.SetValue(entity, 0);
  512. this.Refresh(mode, items as IEnumerable);
  513. }
  514. /// <summary>
  515. /// Refresh a set of objects using the mode specified. If the refresh
  516. /// cannot be performed (for example if the object no longer exists in the
  517. /// database) an InvalidOperationException is thrown.
  518. /// </summary>
  519. /// <param name="mode">How the refresh should be performed.</param>
  520. /// <param name="entities">The objects to refresh.</param>
  521. public void Refresh(RefreshMode mode, params object[] entities)
  522. {
  523. CheckDispose(); // code hygeine requirement
  524. if (entities == null){
  525. throw Error.ArgumentNull("entities");
  526. }
  527. Refresh(mode, (IEnumerable)entities);
  528. }
  529. /// <summary>
  530. /// Refresh a collection of objects using the mode specified. If the refresh
  531. /// cannot be performed (for example if the object no longer exists in the
  532. /// database) an InvalidOperationException is thrown.
  533. /// </summary>
  534. /// <param name="mode">How the refresh should be performed.</param>
  535. /// <param name="entities">The collection of objects to refresh.</param>
  536. public void Refresh(RefreshMode mode, IEnumerable entities)
  537. {
  538. CheckDispose();
  539. CheckNotInSubmitChanges();
  540. VerifyTrackingEnabled();
  541. if (entities == null) {
  542. throw Error.ArgumentNull("entities");
  543. }
  544. // if the collection is a query, we need to execute and buffer,
  545. // since below we will be issuing additional queries and can only
  546. // have a single reader open.
  547. var list = entities.Cast<object>().ToList();
  548. // create a fresh context to fetch new state from
  549. DataContext refreshContext = this.CreateRefreshContext();
  550. foreach (object o in list) {
  551. // verify that each object in the list is an entity
  552. MetaType inheritanceRoot = services.Model.GetMetaType(o.GetType()).InheritanceRoot;
  553. GetTable(inheritanceRoot.Type);
  554. TrackedObject trackedObject = this.services.ChangeTracker.GetTrackedObject(o);
  555. if (trackedObject == null) {
  556. throw Error.UnrecognizedRefreshObject();
  557. }
  558. if (trackedObject.IsNew) {
  559. throw Error.RefreshOfNewObject();
  560. }
  561. // query to get the current database values
  562. object[] keyValues = CommonDataServices.GetKeyValues(trackedObject.Type, trackedObject.Original);
  563. object freshInstance = refreshContext.Services.GetObjectByKey(trackedObject.Type, keyValues);
  564. if (freshInstance == null) {
  565. throw Error.RefreshOfDeletedObject();
  566. }
  567. // refresh the tracked object using the new values and
  568. // the mode specified.
  569. trackedObject.Refresh(mode, freshInstance);
  570. }
  571. }
  572. internal DataContext CreateRefreshContext() {
  573. CheckDispose();
  574. return new DataContext(this);
  575. }
  576. private void AcceptChanges() {
  577. CheckDispose();
  578. VerifyTrackingEnabled();
  579. this.services.ChangeTracker.AcceptChanges();
  580. }
  581. /// <summary>
  582. /// Returns the query text in the database server's native query language
  583. /// that would need to be executed to perform the specified query.
  584. /// </summary>
  585. /// <param name="query">The query</param>
  586. /// <returns></returns>
  587. internal string GetQueryText(IQueryable query) {
  588. CheckDispose();
  589. if (query == null) {
  590. throw Error.ArgumentNull("query");
  591. }
  592. return this.provider.GetQueryText(query.Expression);
  593. }
  594. /// <summary>
  595. /// Returns an IDbCommand object representing the query in the database server's
  596. /// native query language.
  597. /// </summary>
  598. /// <param name="query"></param>
  599. /// <returns></returns>
  600. public DbCommand GetCommand(IQueryable query) {
  601. CheckDispose();
  602. if (query == null) {
  603. throw Error.ArgumentNull("query");
  604. }
  605. return this.provider.GetCommand(query.Expression);
  606. }
  607. /// <summary>
  608. /// Returns the command text in the database server's native query langauge
  609. /// that would need to be executed in order to persist the changes made to the
  610. /// objects back into the database.
  611. /// </summary>
  612. /// <returns></returns>
  613. internal string GetChangeText() {
  614. CheckDispose();
  615. VerifyTrackingEnabled();
  616. return new ChangeProcessor(services, this).GetChangeText();
  617. }
  618. /// <summary>
  619. /// Computes the un-ordered set of objects that have changed
  620. /// </summary>
  621. /// <returns></returns>
  622. [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "ChangeSet", Justification="The capitalization was deliberately chosen.")]
  623. [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Non-trivial operations are not suitable for properties.")]
  624. public ChangeSet GetChangeSet() {
  625. CheckDispose();
  626. return new ChangeProcessor(this.services, this).GetChangeSet();
  627. }
  628. /// <summary>
  629. /// Execute a command against the database server that does not return a sequence of objects.
  630. /// The command is specified using the server's native query language, such as SQL.
  631. /// </summary>
  632. /// <param name="command">The command specified in the server's native query language.</param>
  633. /// <param name="parameters">The parameter values to use for the query.</param>
  634. /// <returns>A single integer return value</returns>
  635. public int ExecuteCommand(string command, params object[] parameters) {
  636. CheckDispose();
  637. if (command == null) {
  638. throw Error.ArgumentNull("command");
  639. }
  640. if (parameters == null) {
  641. throw Error.ArgumentNull("parameters");
  642. }
  643. return (int)this.ExecuteMethodCall(this, (MethodInfo)MethodInfo.GetCurrentMethod(), command, parameters).ReturnValue;
  644. }
  645. /// <summary>
  646. /// Execute the sequence returning query against the database server.
  647. /// The query is specified using the server's native query language, such as SQL.
  648. /// </summary>
  649. /// <typeparam name="TResult">The element type of the result sequence.</typeparam>
  650. /// <param name="query">The query specified in the server's native query language.</param>
  651. /// <param name="parameters">The parameter values to use for the query.</param>
  652. /// <returns>An IEnumerable sequence of objects.</returns>
  653. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  654. public IEnumerable<TResult> ExecuteQuery<TResult>(string query, params object[] parameters) {
  655. CheckDispose();
  656. if (query == null) {
  657. throw Error.ArgumentNull("query");
  658. }
  659. if (parameters == null) {
  660. throw Error.ArgumentNull("parameters");
  661. }
  662. return (IEnumerable<TResult>)this.ExecuteMethodCall(this, ((MethodInfo)MethodInfo.GetCurrentMethod()).MakeGenericMethod(typeof(TResult)), query, parameters).ReturnValue;
  663. }
  664. /// <summary>
  665. /// Execute the sequence returning query against the database server.
  666. /// The query is specified using the server's native query language, such as SQL.
  667. /// </summary>
  668. /// <param name="elementType">The element type of the result sequence.</param>
  669. /// <param name="query">The query specified in the server's native query language.</param>
  670. /// <param name="parameters">The parameter values to use for the query.</param>
  671. /// <returns></returns>
  672. public IEnumerable ExecuteQuery(Type elementType, string query, params object[] parameters) {
  673. CheckDispose();
  674. if (elementType == null) {
  675. throw Error.ArgumentNull("elementType");
  676. }
  677. if (query == null) {
  678. throw Error.ArgumentNull("query");
  679. }
  680. if (parameters == null) {
  681. throw Error.ArgumentNull("parameters");
  682. }
  683. if (_miExecuteQuery == null) {
  684. _miExecuteQuery = typeof(DataContext).GetMethods().Single(m => m.Name == "ExecuteQuery" && m.GetParameters().Length == 2);
  685. }
  686. return (IEnumerable)this.ExecuteMethodCall(this, _miExecuteQuery.MakeGenericMethod(elementType), query, parameters).ReturnValue;
  687. }
  688. private static MethodInfo _miExecuteQuery;
  689. /// <summary>
  690. /// Executes the equivalent of the specified method call on the database server.
  691. /// </summary>
  692. /// <param name="instance">The instance the method is being called on.</param>
  693. /// <param name="methodInfo">The reflection MethodInfo for the method to invoke.</param>
  694. /// <param name="parameters">The parameters for the method call.</param>
  695. /// <returns>The result of the method call. Use this type's ReturnValue property to access the actual return value.</returns>
  696. internal protected IExecuteResult ExecuteMethodCall(object instance, MethodInfo methodInfo, params object[] parameters) {
  697. CheckDispose();
  698. if (instance == null) {
  699. throw Error.ArgumentNull("instance");
  700. }
  701. if (methodInfo == null) {
  702. throw Error.ArgumentNull("methodInfo");
  703. }
  704. if (parameters == null) {
  705. throw Error.ArgumentNull("parameters");
  706. }
  707. return this.provider.Execute(this.GetMethodCall(instance, methodInfo, parameters));
  708. }
  709. /// <summary>
  710. /// Create a query object for the specified method call.
  711. /// </summary>
  712. /// <typeparam name="TResult">The element type of the query.</typeparam>
  713. /// <param name="instance">The instance the method is being called on.</param>
  714. /// <param name="methodInfo">The reflection MethodInfo for the method to invoke.</param>
  715. /// <param name="parameters">The parameters for the method call.</param>
  716. /// <returns>The returned query object</returns>
  717. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  718. internal protected IQueryable<TResult> CreateMethodCallQuery<TResult>(object instance, MethodInfo methodInfo, params object[] parameters) {
  719. CheckDispose();
  720. if (instance == null) {
  721. throw Error.ArgumentNull("instance");
  722. }
  723. if (methodInfo == null) {
  724. throw Error.ArgumentNull("methodInfo");
  725. }
  726. if (parameters == null) {
  727. throw Error.ArgumentNull("parameters");
  728. }
  729. if (!typeof(IQueryable<TResult>).IsAssignableFrom(methodInfo.ReturnType)) {
  730. throw Error.ExpectedQueryableArgument("methodInfo", typeof(IQueryable<TResult>));
  731. }
  732. return new DataQuery<TResult>(this, this.GetMethodCall(instance, methodInfo, parameters));
  733. }
  734. private Expression GetMethodCall(object instance, MethodInfo methodInfo, params object[] parameters) {
  735. CheckDispose();
  736. if (parameters.Length > 0) {
  737. ParameterInfo[] pis = methodInfo.GetParameters();
  738. List<Expression> args = new List<Expression>(parameters.Length);
  739. for (int i = 0, n = parameters.Length; i < n; i++) {
  740. Type pType = pis[i].ParameterType;
  741. if (pType.IsByRef) {
  742. pType = pType.GetElementType();
  743. }
  744. args.Add(Expression.Constant(parameters[i], pType));
  745. }
  746. return Expression.Call(Expression.Constant(instance), methodInfo, args);
  747. }
  748. return Expression.Call(Expression.Constant(instance), methodInfo);
  749. }
  750. /// <summary>
  751. /// Execute a dynamic insert
  752. /// </summary>
  753. /// <param name="entity"></param>
  754. internal protected void ExecuteDynamicInsert(object entity) {
  755. CheckDispose();
  756. if (entity == null) {
  757. throw Error.ArgumentNull("entity");
  758. }
  759. this.CheckInSubmitChanges();
  760. TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
  761. if (tracked == null) {
  762. throw Error.CannotPerformOperationForUntrackedObject();
  763. }
  764. this.services.ChangeDirector.DynamicInsert(tracked);
  765. }
  766. /// <summary>
  767. /// Execute a dynamic update
  768. /// </summary>
  769. /// <param name="entity"></param>
  770. internal protected void ExecuteDynamicUpdate(object entity) {
  771. CheckDispose();
  772. if (entity == null) {
  773. throw Error.ArgumentNull("entity");
  774. }
  775. this.CheckInSubmitChanges();
  776. TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
  777. if (tracked == null) {
  778. throw Error.CannotPerformOperationForUntrackedObject();
  779. }
  780. int result = this.services.ChangeDirector.DynamicUpdate(tracked);
  781. if (result == 0) {
  782. throw new ChangeConflictException();
  783. }
  784. }
  785. /// <summary>
  786. /// Execute a dynamic delete
  787. /// </summary>
  788. /// <param name="entity"></param>
  789. internal protected void ExecuteDynamicDelete(object entity) {
  790. CheckDispose();
  791. if (entity == null) {
  792. throw Error.ArgumentNull("entity");
  793. }
  794. this.CheckInSubmitChanges();
  795. TrackedObject tracked = this.services.ChangeTracker.GetTrackedObject(entity);
  796. if (tracked == null) {
  797. throw Error.CannotPerformOperationForUntrackedObject();
  798. }
  799. int result = this.services.ChangeDirector.DynamicDelete(tracked);
  800. if (result == 0) {
  801. throw new ChangeConflictException();
  802. }
  803. }
  804. /// <summary>
  805. /// Translates the data from a DbDataReader into sequence of objects.
  806. /// </summary>
  807. /// <typeparam name="TResult">The element type of the resulting sequence</typeparam>
  808. /// <param name="reader">The DbDataReader to translate</param>
  809. /// <returns>The translated sequence of objects</returns>
  810. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  811. public IEnumerable<TResult> Translate<TResult>(DbDataReader reader) {
  812. CheckDispose();
  813. return (IEnumerable<TResult>)this.Translate(typeof(TResult), reader);
  814. }
  815. /// <summary>
  816. /// Translates the data from a DbDataReader into sequence of objects.
  817. /// </summary>
  818. /// <param name="elementType">The element type of the resulting sequence</param>
  819. /// <param name="reader">The DbDataReader to translate</param>
  820. /// <returns>The translated sequence of objects</returns>
  821. public IEnumerable Translate(Type elementType, DbDataReader reader) {
  822. CheckDispose();
  823. if (elementType == null) {
  824. throw Error.ArgumentNull("elementType");
  825. }
  826. if (reader == null) {
  827. throw Error.ArgumentNull("reader");
  828. }
  829. return this.provider.Translate(elementType, reader);
  830. }
  831. /// <summary>
  832. /// Translates the data from a DbDataReader into IMultipleResults.
  833. /// </summary>
  834. /// <param name="reader">The DbDataReader to translate</param>
  835. /// <returns>The translated sequence of objects</returns>
  836. public IMultipleResults Translate(DbDataReader reader) {
  837. CheckDispose();
  838. if (reader == null) {
  839. throw Error.ArgumentNull("reader");
  840. }
  841. return this.provider.Translate(reader);
  842. }
  843. /// <summary>
  844. /// Remove all Include\Subquery LoadOptions settings.
  845. /// </summary>
  846. internal void ResetLoadOptions() {
  847. CheckDispose();
  848. this.loadOptions = null;
  849. }
  850. /// <summary>
  851. /// The DataLoadOptions used to define prefetch behavior for defer loaded members
  852. /// and membership of related collections.
  853. /// </summary>
  854. public DataLoadOptions LoadOptions {
  855. get {
  856. CheckDispose();
  857. return this.loadOptions;
  858. }
  859. set {
  860. CheckDispose();
  861. if (this.services.HasCachedObjects && value != this.loadOptions) {
  862. throw Error.LoadOptionsChangeNotAllowedAfterQuery();
  863. }
  864. if (value != null) {
  865. value.Freeze();
  866. }
  867. this.loadOptions = value;
  868. }
  869. }
  870. /// <summary>
  871. /// This list of change conflicts produced by the last call to SubmitChanges. Use this collection
  872. /// to resolve conflicts after catching a ChangeConflictException and before calling SubmitChanges again.
  873. /// </summary>
  874. public ChangeConflictCollection ChangeConflicts {
  875. get {
  876. CheckDispose();
  877. return this.conflicts;
  878. }
  879. }
  880. }
  881. /// <summary>
  882. /// Defines behavior for implementations of IQueryable that allow modifications to the membership of the resulting set.
  883. /// </summary>
  884. /// <typeparam name="TEntity">Type of entities returned from the queryable.</typeparam>
  885. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
  886. public interface ITable<TEntity> : IQueryable<TEntity>
  887. where TEntity : class
  888. {
  889. /// <summary>
  890. /// Notify the set that an object representing a new entity should be added to the set.
  891. /// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
  892. /// until changes to that set have been persisted in some manner.
  893. /// </summary>
  894. /// <param name="entity">Entity object to be added.</param>
  895. void InsertOnSubmit(TEntity entity);
  896. /// <summary>
  897. /// Notify the set that an object representing a new entity should be added to the set.
  898. /// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
  899. /// until changes to that set have been persisted in some manner.
  900. /// </summary>
  901. /// <param name="entity">Entity object to be attached.</param>
  902. void Attach(TEntity entity);
  903. /// <summary>
  904. /// Notify the set that an object representing an entity should be removed from the set.
  905. /// Depending on the implementation, the change to the set may not be visible in an enumeration of the set
  906. /// until changes to that set have been persisted in some manner.
  907. /// </summary>
  908. /// <param name="entity">Entity object to be removed.</param>
  909. /// <exception cref="InvalidOperationException">Throws if the specified object is not in the set.</exception>
  910. void DeleteOnSubmit(TEntity entity);
  911. }
  912. /// <summary>
  913. /// ITable is the common interface for DataContext tables. It can be used as the source
  914. /// of a dynamic/runtime-generated query.
  915. /// </summary>
  916. [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification="[....]: Meant to represent a database table which is delayed loaded and doesn't provide collection semantics.")]
  917. public interface ITable : IQueryable {
  918. /// <summary>
  919. /// The DataContext containing this Table.
  920. /// </summary>
  921. DataContext Context { get; }
  922. /// <summary>
  923. /// Adds an entity in a 'pending insert' state to this table. The added entity will not be observed
  924. /// in query results from this table until after SubmitChanges has been called. Any untracked
  925. /// objects referenced directly or transitively by the entity will also be inserted.
  926. /// </summary>
  927. /// <param name="entity"></param>
  928. void InsertOnSubmit(object entity);
  929. /// <summary>
  930. /// Adds all entities of a collection to the DataContext in a 'pending insert' state.
  931. /// The added entities will not be observed in query results until after SubmitChanges()
  932. /// has been called. Any untracked objects referenced directly or transitively by the
  933. /// the inserted entities will also be inserted.
  934. /// </summary>
  935. /// <param name="entities"></param>
  936. void InsertAllOnSubmit(IEnumerable entities);
  937. /// <summary>
  938. /// Attaches an entity to the DataContext in an unmodified state, similiar to as if it had been
  939. /// retrieved via a query. Other entities accessible from this entity are attached as unmodified
  940. /// but may subsequently be transitioned to other states by performing table operations on them
  941. /// individually.
  942. /// </summary>
  943. /// <param name="entity"></param>
  944. void Attach(object entity);
  945. /// <summary>
  946. /// Attaches an entity to the DataContext in either a modified or unmodified state.
  947. /// If attaching as modified, the entity must either declare a version member or must
  948. /// not participate in update conflict checking. Other entities accessible from this
  949. /// entity are attached as unmodified but may subsequently be transitioned to other
  950. /// states by performing table operations on them individually.
  951. /// </summary>
  952. /// <param name="entity"></param>
  953. /// <param name="asModified"></param>
  954. void Attach(object entity, bool asModified);
  955. /// <summary>
  956. /// Attaches an entity to the DataContext in either a modified or unmodified state by specifying both the entity
  957. /// and its original state. Other entities accessible from this
  958. /// entity are attached as unmodified but may subsequently be transitioned to other
  959. /// states by performing table operations on them individually.
  960. /// </summary>
  961. /// <param name="entity">The entity to attach.</param>
  962. /// <param name="original">An instance of the same entity type with data members containing
  963. /// the original values.</param>
  964. void Attach(object entity, object original);
  965. /// <summary>
  966. /// Attaches all entities of a collection to the DataContext in an unmodified state,
  967. /// similiar to as if each had been retrieved via a query. Other entities accessible from these
  968. /// entities are attached as unmodified but may subsequently be transitioned to other
  969. /// states by performing table operations on them individually.
  970. /// </summary>
  971. /// <param name="entities"></param>
  972. void AttachAll(IEnumerable entities);
  973. /// <summary>
  974. /// Attaches all entities of a collection to the DataContext in either a modified or unmodified state.
  975. /// If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking.
  976. /// Other entities accessible from these
  977. /// entities are attached as unmodified but may subsequently be transitioned to other
  978. /// states by performing table operations on them individually.
  979. /// </summary>
  980. /// <param name="entities">The collection of entities.</param>
  981. /// <param name="asModified">True if the entities are to be attach as modified.</param>
  982. void AttachAll(IEnumerable entities, bool asModified);
  983. /// <summary>
  984. /// Puts an entity from this table into a 'pending delete' state. The removed entity will not be observed
  985. /// missing from query results until after SubmitChanges() has been called.
  986. /// </summary>
  987. /// <param name="entity">The entity to remove.</param>
  988. void DeleteOnSubmit(object entity);
  989. /// <summary>
  990. /// Puts all entities from the collection 'entities' into a 'pending delete' state. The removed entities will
  991. /// not be observed missing from the query results until after SubmitChanges() is called.
  992. /// </summary>
  993. /// <param name="entities"></param>
  994. void DeleteAllOnSubmit(IEnumerable entities);
  995. /// <summary>
  996. /// Returns an instance containing the original state of the entity.
  997. /// </summary>
  998. /// <param name="entity"></param>
  999. /// <returns></returns>
  1000. object GetOriginalEntityState(object entity);
  1001. /// <summary>
  1002. /// Returns an array of modified members containing their current and original values
  1003. /// for the entity specified.
  1004. /// </summary>
  1005. /// <param name="entity"></param>
  1006. /// <returns></returns>
  1007. ModifiedMemberInfo[] GetModifiedMembers(object entity);
  1008. /// <summary>
  1009. /// True if the table is read-only.
  1010. /// </summary>
  1011. bool IsReadOnly { get; }
  1012. }
  1013. /// <summary>
  1014. /// Table is a collection of persistent entities. It always contains the set of entities currently
  1015. /// persisted in the database. Use it as a source of queries and to add/insert and remove/delete entities.
  1016. /// </summary>
  1017. /// <typeparam name="TEntity"></typeparam>
  1018. [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification="[....]: Meant to represent a database table which is delayed loaded and doesn't provide collection semantics.")]
  1019. public sealed class Table<TEntity> : IQueryable<TEntity>, IQueryProvider, IEnumerable<TEntity>, IQueryable, IEnumerable, ITable, IListSource, ITable<TEntity>
  1020. where TEntity : class {
  1021. DataContext context;
  1022. MetaTable metaTable;
  1023. internal Table(DataContext context, MetaTable metaTable) {
  1024. System.Diagnostics.Debug.Assert(metaTable != null);
  1025. this.context = context;
  1026. this.metaTable = metaTable;
  1027. }
  1028. /// <summary>
  1029. /// The DataContext containing this Table.
  1030. /// </summary>
  1031. public DataContext Context {
  1032. get { return this.context; }
  1033. }
  1034. /// <summary>
  1035. /// True if the table is read-only.
  1036. /// </summary>
  1037. public bool IsReadOnly {
  1038. get { return !metaTable.RowType.IsEntity; }
  1039. }
  1040. Expression IQueryable.Expression {
  1041. get { return Expression.Constant(this); }
  1042. }
  1043. Type IQueryable.ElementType {
  1044. get { return typeof(TEntity); }
  1045. }
  1046. IQueryProvider IQueryable.Provider{
  1047. get{
  1048. return (IQueryProvider)this;
  1049. }
  1050. }
  1051. [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
  1052. IQueryable IQueryProvider.CreateQuery(Expression expression) {
  1053. if (expression == null) {
  1054. throw Error.ArgumentNull("expression");
  1055. }
  1056. Type eType = System.Data.Linq.SqlClient.TypeSystem.GetElementType(expression.Type);
  1057. Type qType = typeof(IQueryable<>).MakeGenericType(eType);
  1058. if (!qType.IsAssignableFrom(expression.Type)) {
  1059. throw Error.ExpectedQueryableArgument("expression", qType);
  1060. }
  1061. Type dqType = typeof(DataQuery<>).MakeGenericType(eType);
  1062. return (IQueryable)Activator.CreateInstance(dqType, new object[] { this.context, expression });
  1063. }
  1064. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  1065. IQueryable<TResult> IQueryProvider.CreateQuery<TResult>(Expression expression) {
  1066. if (expression == null) {
  1067. throw Error.ArgumentNull("expression");
  1068. }
  1069. if (!typeof(IQueryable<TResult>).IsAssignableFrom(expression.Type)) {
  1070. throw Error.ExpectedQueryableArgument("expression", typeof(IEnumerable<TResult>));
  1071. }
  1072. return new DataQuery<TResult>(this.context, expression);
  1073. }
  1074. object IQueryProvider.Execute(Expression expression) {
  1075. return this.context.Provider.Execute(expression).ReturnValue;
  1076. }
  1077. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "[....]: Generic parameters are required for strong-typing of the return type.")]
  1078. TResult IQueryProvider.Execute<TResult>(Expression expression) {
  1079. return (TResult)this.context.Provider.Execute(expression).ReturnValue;
  1080. }
  1081. IEnumerator IEnumerable.GetEnumerator() {
  1082. return this.GetEnumerator();
  1083. }
  1084. IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator() {
  1085. return this.GetEnumerator();
  1086. }
  1087. public IEnumerator<TEntity> GetEnumerator() {
  1088. return ((IEnumerable<TEntity>)this.context.Provider.Execute(Expression.Constant(this)).ReturnValue).GetEnumerator();
  1089. }
  1090. bool IListSource.ContainsListCollection {
  1091. get { return false; }
  1092. }
  1093. private IBindingList cachedList;
  1094. IList IListSource.GetList() {
  1095. if (cachedList == null) {
  1096. cachedList = GetNewBindingList();
  1097. }
  1098. return cachedList;
  1099. }
  1100. [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification="Method doesn't represent a property of the type.")]
  1101. public IBindingList GetNewBindingList() {
  1102. return BindingList.Create<TEntity>(this.context, this);
  1103. }
  1104. /// <summary>
  1105. /// Adds an entity in a 'pending insert' state to this table. The added entity will not be observed
  1106. /// in query results from this table until after SubmitChanges() has been called. Any untracked
  1107. /// objects referenced directly or transitively by the entity will also be inserted.
  1108. /// </summary>
  1109. /// <param name="entity"></param>
  1110. public void InsertOnSubmit(TEntity entity) {
  1111. if (entity == null) {
  1112. throw Error.ArgumentNull("entity");
  1113. }
  1114. CheckReadOnly();
  1115. context.CheckNotInSubmitChanges();
  1116. context.VerifyTrackingEnabled();
  1117. MetaType type = this.metaTable.RowType.GetInheritanceType(entity.GetType());
  1118. if (!IsTrackableType(type)) {
  1119. throw Error.TypeCouldNotBeAdded(type.Type);
  1120. }
  1121. TrackedObject tracked = this.context.Services.ChangeTracker.GetTrackedObject(entity);
  1122. if (tracked == null) {
  1123. tracked = this.context.Services.ChangeTracker.Track(entity);
  1124. tracked.ConvertToNew();
  1125. } else if (tracked.IsWeaklyTracked) {
  1126. tracked.ConvertToNew();
  1127. } else if (tracked.IsDeleted) {
  1128. tracked.ConvertToPossiblyModified();
  1129. } else if (tracked.IsRemoved) {
  1130. tracked.ConvertToNew();
  1131. } else if (!tracked.IsNew) {
  1132. throw Error.CantAddAlreadyExistingItem();
  1133. }
  1134. }
  1135. void ITable.InsertOnSubmit(object entity) {
  1136. if (entity == null) {
  1137. throw Error.ArgumentNull("entity");
  1138. }
  1139. TEntity tEntity = entity as TEntity;
  1140. if (tEntity == null) {
  1141. throw Error.EntityIsTheWrongType();
  1142. }
  1143. this.InsertOnSubmit(tEntity);
  1144. }
  1145. /// <summary>
  1146. /// Adds all entities of a collection to the DataContext in a 'pending insert' state.
  1147. /// The added entities will not be observed in query results until after SubmitChanges()
  1148. /// has been called.
  1149. /// </summary>
  1150. /// <param name="entities"></param>
  1151. public void InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity {
  1152. if (entities == null) {
  1153. throw Error.ArgumentNull("entities");
  1154. }
  1155. CheckReadOnly();
  1156. context.CheckNotInSubmitChanges();
  1157. context.VerifyTrackingEnabled();
  1158. List<TSubEntity> list = entities.ToList();
  1159. foreach (TEntity entity in list) {
  1160. this.InsertOnSubmit(entity);
  1161. }
  1162. }
  1163. void ITable.InsertAllOnSubmit(IEnumerable entities) {
  1164. if (entities == null) {
  1165. throw Error.ArgumentNull("entities");
  1166. }
  1167. CheckReadOnly();
  1168. context.CheckNotInSubmitChanges();
  1169. context.VerifyTrackingEnabled();
  1170. List<object> list = entities.Cast<object>().ToList();
  1171. ITable itable = this;
  1172. foreach (object entity in list) {
  1173. itable.InsertOnSubmit(entity);
  1174. }
  1175. }
  1176. /// <summary>
  1177. /// Returns true if this specific type is mapped into the database.
  1178. /// For example, an abstract type can't be present because it can not be instantiated.
  1179. /// </summary>
  1180. private static bool IsTrackableType(MetaType type) {
  1181. if (type == null) {
  1182. return false;
  1183. }
  1184. if (!type.CanInstantiate) {
  1185. return false;
  1186. }
  1187. if (type.HasInheritance && !type.HasInheritanceCode) {
  1188. return false;
  1189. }
  1190. return true;
  1191. }
  1192. /// <summary>
  1193. /// Puts an entity from this table into a 'pending delete' state. The removed entity will not be observed
  1194. /// missing from query results until after SubmitChanges() has been called.
  1195. /// </summary>
  1196. /// <param name="item"></param>
  1197. public void DeleteOnSubmit(TEntity entity) {
  1198. if (entity == null) {
  1199. throw Error.ArgumentNull("entity");
  1200. }
  1201. CheckReadOnly();
  1202. context.CheckNotInSubmitChanges();
  1203. context.VerifyTrackingEnabled();
  1204. TrackedObject tracked = this.context.Services.ChangeTracker.GetTrackedObject(entity);
  1205. if (tracked != null) {
  1206. if (tracked.IsNew) {
  1207. tracked.ConvertToRemoved();
  1208. }
  1209. else if (tracked.IsPossiblyModified || tracked.IsModified) {
  1210. tracked.ConvertToDeleted();
  1211. }
  1212. }
  1213. else {
  1214. throw Error.CannotRemoveUnattachedEntity();
  1215. }
  1216. }
  1217. void ITable.DeleteOnSubmit(object entity) {
  1218. if (entity == null) {
  1219. throw Error.ArgumentNull("entity");
  1220. }
  1221. TEntity tEntity = entity as TEntity;
  1222. if (tEntity == null) {
  1223. throw Error.EntityIsTheWrongType();
  1224. }
  1225. this.DeleteOnSubmit(tEntity);
  1226. }
  1227. /// <summary>
  1228. /// Puts all entities from the collection 'entities' into a 'pending delete' state. The removed entities will
  1229. /// not be observed missing from the query results until after SubmitChanges() is called.
  1230. /// </summary>
  1231. /// <param name="entities"></param>
  1232. public void DeleteAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity {
  1233. if (entities == null) {
  1234. throw Error.ArgumentNull("entities");
  1235. }
  1236. CheckReadOnly();
  1237. context.CheckNotInSubmitChanges();
  1238. context.VerifyTrackingEnabled();
  1239. List<TSubEntity> list = entities.ToList();
  1240. foreach (TEntity entity in list) {
  1241. this.DeleteOnSubmit(entity);
  1242. }
  1243. }
  1244. void ITable.DeleteAllOnSubmit(IEnumerable entities) {
  1245. if (entities == null) {
  1246. throw Error.ArgumentNull("entities");
  1247. }
  1248. CheckReadOnly();
  1249. context.CheckNotInSubmitChanges();
  1250. context.VerifyTrackingEnabled();
  1251. List<object> list = entities.Cast<object>().ToList();
  1252. ITable itable = this;
  1253. foreach (object entity in list) {
  1254. itable.DeleteOnSubmit(entity);
  1255. }
  1256. }
  1257. /// <summary>
  1258. /// Attaches an entity to the DataContext in an unmodified state, similiar to as if it had been
  1259. /// retrieved via a query. Deferred loading is not enabled. Other entities accessible from this
  1260. /// entity are not automatically attached.
  1261. /// </summary>
  1262. /// <param name="entity"></param>
  1263. public void Attach(TEntity entity) {
  1264. if (entity == null) {
  1265. throw Error.ArgumentNull("entity");
  1266. }
  1267. this.Attach(entity, false);
  1268. }
  1269. void ITable.Attach(object entity) {
  1270. if (entity == null) {
  1271. throw Error.ArgumentNull("entity");
  1272. }
  1273. TEntity tEntity = entity as TEntity;
  1274. if (tEntity == null) {
  1275. throw Error.EntityIsTheWrongType();
  1276. }
  1277. this.Attach(tEntity, false);
  1278. }
  1279. /// <summary>
  1280. /// Attaches an entity to the DataContext in either a modified or unmodified state.
  1281. /// If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking.
  1282. /// Deferred loading is not enabled. Other entities accessible from this entity are not automatically attached.
  1283. /// </summary>
  1284. /// <param name="entity"></param>
  1285. /// <param name="asModified"></param>
  1286. public void Attach(TEntity entity, bool asModified) {
  1287. if (entity == null) {
  1288. throw Error.ArgumentNull("entity");
  1289. }
  1290. CheckReadOnly();
  1291. context.CheckNotInSubmitChanges();
  1292. context.VerifyTrackingEnabled();
  1293. MetaType type = this.metaTable.RowType.GetInheritanceType(entity.GetType());
  1294. if (!IsTrackableType(type)) {
  1295. throw Error.TypeCouldNotBeTracked(type.Type);
  1296. }
  1297. if (asModified) {
  1298. bool canAttach = type.VersionMember != null || !type.HasUpdateCheck;
  1299. if (!canAttach) {
  1300. throw Error.CannotAttachAsModifiedWithoutOriginalState();
  1301. }
  1302. }
  1303. TrackedObject tracked = this.Context.Services.ChangeTracker.GetTrackedObject(entity);
  1304. if (tracked == null || tracked.IsWeaklyTracked) {
  1305. if (tracked == null) {
  1306. tracked = this.context.Services.ChangeTracker.Track(entity, true);
  1307. }
  1308. if (asModified) {
  1309. tracked.ConvertToModified();
  1310. } else {
  1311. tracked.ConvertToUnmodified();
  1312. }
  1313. if (this.Context.Services.InsertLookupCachedObject(type, entity) != entity) {
  1314. throw new DuplicateKeyException(entity, Strings.CantAddAlreadyExistingKey);
  1315. }
  1316. tracked.InitializeDeferredLoaders();
  1317. }
  1318. else {
  1319. throw Error.CannotAttachAlreadyExistingEntity();
  1320. }
  1321. }
  1322. void ITable.Attach(object entity, bool asModified) {
  1323. if (entity == null) {
  1324. throw Error.ArgumentNull("entity");
  1325. }
  1326. TEntity tEntity = entity as TEntity;
  1327. if (tEntity == null) {
  1328. throw Error.EntityIsTheWrongType();
  1329. }
  1330. this.Attach(tEntity, asModified);
  1331. }
  1332. /// <summary>
  1333. /// Attaches an entity to the DataContext in either a modified or unmodified state by specifying both the entity
  1334. /// and its original state.
  1335. /// </summary>
  1336. /// <param name="entity">The entity to attach.</param>
  1337. /// <param name="original">An instance of the same entity type with data members containing
  1338. /// the original values.</param>
  1339. public void Attach(TEntity entity, TEntity original) {
  1340. if (entity == null) {
  1341. throw Error.ArgumentNull("entity");
  1342. }
  1343. if (original == null) {
  1344. throw Error.ArgumentNull("original");
  1345. }
  1346. if (entity.GetType() != original.GetType()) {
  1347. throw Error.OriginalEntityIsWrongType();
  1348. }
  1349. CheckReadOnly();
  1350. context.CheckNotInSubmitChanges();
  1351. context.VerifyTrackingEnabled();
  1352. MetaType type = this.metaTable.RowType.GetInheritanceType(entity.GetType());
  1353. if (!IsTrackableType(type)) {
  1354. throw Error.TypeCouldNotBeTracked(type.Type);
  1355. }
  1356. TrackedObject tracked = this.context.Services.ChangeTracker.GetTrackedObject(entity);
  1357. if (tracked == null || tracked.IsWeaklyTracked) {
  1358. if (tracked == null) {
  1359. tracked = this.context.Services.ChangeTracker.Track(entity, true);
  1360. }
  1361. tracked.ConvertToPossiblyModified(original);
  1362. if (this.Context.Services.InsertLookupCachedObject(type, entity) != entity) {
  1363. throw new DuplicateKeyException(entity, Strings.CantAddAlreadyExistingKey);
  1364. }
  1365. tracked.InitializeDeferredLoaders();
  1366. }
  1367. else {
  1368. throw Error.CannotAttachAlreadyExistingEntity();
  1369. }
  1370. }
  1371. void ITable.Attach(object entity, object original) {
  1372. if (entity == null) {
  1373. throw Error.ArgumentNull("entity");
  1374. }
  1375. if (original == null) {
  1376. throw Error.ArgumentNull("original");
  1377. }
  1378. CheckReadOnly();
  1379. context.CheckNotInSubmitChanges();
  1380. context.VerifyTrackingEnabled();
  1381. TEntity tEntity = entity as TEntity;
  1382. if (tEntity == null) {
  1383. throw Error.EntityIsTheWrongType();
  1384. }
  1385. if (entity.GetType() != original.GetType()) {
  1386. throw Error.OriginalEntityIsWrongType();
  1387. }
  1388. this.Attach(tEntity, (TEntity)original);
  1389. }
  1390. /// <summary>
  1391. /// Attaches all entities of a collection to the DataContext in an unmodified state,
  1392. /// similiar to as if each had been retrieved via a query. Deferred loading is not enabled.
  1393. /// Other entities accessible from these entities are not automatically attached.
  1394. /// </summary>
  1395. /// <param name="entities"></param>
  1396. public void AttachAll<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity {
  1397. if (entities == null) {
  1398. throw Error.ArgumentNull("entities");
  1399. }
  1400. this.AttachAll(entities, false);
  1401. }
  1402. void ITable.AttachAll(IEnumerable entities) {
  1403. if (entities == null) {
  1404. throw Error.ArgumentNull("entities");
  1405. }
  1406. ((ITable)this).AttachAll(entities, false);
  1407. }
  1408. /// <summary>
  1409. /// Attaches all entities of a collection to the DataContext in either a modified or unmodified state.
  1410. /// If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking.
  1411. /// Deferred loading is not enabled. Other entities accessible from these entities are not automatically attached.
  1412. /// </summary>
  1413. /// <param name="entities">The collection of entities.</param>
  1414. /// <param name="asModified">True if the entities are to be attach as modified.</param>
  1415. public void AttachAll<TSubEntity>(IEnumerable<TSubEntity> entities, bool asModified) where TSubEntity : TEntity {
  1416. if (entities == null) {
  1417. throw Error.ArgumentNull("entities");
  1418. }
  1419. CheckReadOnly();
  1420. context.CheckNotInSubmitChanges();
  1421. context.VerifyTrackingEnabled();
  1422. List<TSubEntity> list = entities.ToList();
  1423. foreach (TEntity entity in list) {
  1424. this.Attach(entity, asModified);
  1425. }
  1426. }
  1427. void ITable.AttachAll(IEnumerable entities, bool asModified) {
  1428. if (entities == null) {
  1429. throw Error.ArgumentNull("entities");
  1430. }
  1431. CheckReadOnly();
  1432. context.CheckNotInSubmitChanges();
  1433. context.VerifyTrackingEnabled();
  1434. List<object> list = entities.Cast<object>().ToList();
  1435. ITable itable = this;
  1436. foreach (object entity in list) {
  1437. itable.Attach(entity, asModified);
  1438. }
  1439. }
  1440. /// <summary>
  1441. /// Returns an instance containing the original state of the entity.
  1442. /// </summary>
  1443. /// <param name="entity"></param>
  1444. /// <returns></returns>
  1445. public TEntity GetOriginalEntityState(TEntity entity) {
  1446. if (entity == null) {
  1447. throw Error.ArgumentNull("entity");
  1448. }
  1449. MetaType type = this.Context.Mapping.GetMetaType(entity.GetType());
  1450. if (type == null || !type.IsEntity) {
  1451. throw Error.EntityIsTheWrongType();
  1452. }
  1453. TrackedObject tracked = this.Context.Services.ChangeTracker.GetTrackedObject(entity);
  1454. if (tracked != null) {
  1455. if (tracked.Original != null) {
  1456. return (TEntity) tracked.CreateDataCopy(tracked.Original);
  1457. }
  1458. else {
  1459. return (TEntity) tracked.CreateDataCopy(tracked.Current);
  1460. }
  1461. }
  1462. return null;
  1463. }
  1464. object ITable.GetOriginalEntityState(object entity) {
  1465. if (entity == null) {
  1466. throw Error.ArgumentNull("entity");
  1467. }
  1468. TEntity tEntity = entity as TEntity;
  1469. if (tEntity == null) {
  1470. throw Error.EntityIsTheWrongType();
  1471. }
  1472. return this.GetOriginalEntityState(tEntity);
  1473. }
  1474. /// <summary>
  1475. /// Returns an array of modified members containing their current and original values
  1476. /// for the entity specified.
  1477. /// </summary>
  1478. /// <param name="entity"></param>
  1479. /// <returns></returns>
  1480. public ModifiedMemberInfo[] GetModifiedMembers(TEntity entity) {
  1481. if (entity == null) {
  1482. throw Error.ArgumentNull("entity");
  1483. }
  1484. MetaType type = this.Context.Mapping.GetMetaType(entity.GetType());
  1485. if (type == null || !type.IsEntity) {
  1486. throw Error.EntityIsTheWrongType();
  1487. }
  1488. TrackedObject tracked = this.Context.Services.ChangeTracker.GetTrackedObject(entity);
  1489. if (tracked != null) {
  1490. return tracked.GetModifiedMembers().ToArray();
  1491. }
  1492. return new ModifiedMemberInfo[] { };
  1493. }
  1494. ModifiedMemberInfo[] ITable.GetModifiedMembers(object entity) {
  1495. if (entity == null) {
  1496. throw Error.ArgumentNull("entity");
  1497. }
  1498. TEntity tEntity = entity as TEntity;
  1499. if (tEntity == null) {
  1500. throw Error.EntityIsTheWrongType();
  1501. }
  1502. return this.GetModifiedMembers(tEntity);
  1503. }
  1504. private void CheckReadOnly() {
  1505. if (this.IsReadOnly) {
  1506. throw Error.CannotPerformCUDOnReadOnlyTable(ToString());
  1507. }
  1508. }
  1509. public override string ToString() {
  1510. return "Table(" + typeof(TEntity).Name + ")";
  1511. }
  1512. }
  1513. [SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "ChangeSet", Justification="The capitalization was deliberately chosen.")]
  1514. public sealed class ChangeSet {
  1515. ReadOnlyCollection<object> inserts;
  1516. ReadOnlyCollection<object> deletes;
  1517. ReadOnlyCollection<object> updates;
  1518. internal ChangeSet(
  1519. ReadOnlyCollection<object> inserts,
  1520. ReadOnlyCollection<object> deletes,
  1521. ReadOnlyCollection<object> updates
  1522. ) {
  1523. this.inserts = inserts;
  1524. this.deletes = deletes;
  1525. this.updates = updates;
  1526. }
  1527. public IList<object> Inserts {
  1528. get { return this.inserts; }
  1529. }
  1530. public IList<object> Deletes {
  1531. get { return this.deletes; }
  1532. }
  1533. public IList<object> Updates {
  1534. get { return this.updates; }
  1535. }
  1536. public override string ToString() {
  1537. return "{" +
  1538. string.Format(
  1539. Globalization.CultureInfo.InvariantCulture,
  1540. "Inserts: {0}, Deletes: {1}, Updates: {2}",
  1541. this.Inserts.Count,
  1542. this.Deletes.Count,
  1543. this.Updates.Count
  1544. ) + "}";
  1545. }
  1546. }
  1547. [SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "[....]: Types are never compared to each other. When comparisons happen it is against the entities that are represented by these constructs.")]
  1548. public struct ModifiedMemberInfo {
  1549. MemberInfo member;
  1550. object current;
  1551. object original;
  1552. internal ModifiedMemberInfo(MemberInfo member, object current, object original) {
  1553. this.member = member;
  1554. this.current = current;
  1555. this.original = original;
  1556. }
  1557. public MemberInfo Member {
  1558. get { return this.member; }
  1559. }
  1560. public object CurrentValue {
  1561. get { return this.current; }
  1562. }
  1563. public object OriginalValue {
  1564. get { return this.original; }
  1565. }
  1566. }
  1567. }