1 //---------------------------------------------------------------------
2 // <copyright file="EntityAdapter.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 namespace System.Data.EntityClient
12 using System.Data.Common;
13 using System.Data.Mapping.Update.Internal;
14 using System.Data.Objects;
15 using System.Diagnostics;
18 /// Class representing a data adapter for the conceptual layer
20 internal sealed class EntityAdapter : IEntityAdapter
22 private bool _acceptChangesDuringUpdate = true;
23 private EntityConnection _connection;
24 private Int32? _commandTimeout;
27 /// Constructs the EntityAdapter object without a connection object
29 public EntityAdapter()
34 /// Gets or sets the map connection used by this adapter.
36 DbConnection IEntityAdapter.Connection
40 return this.Connection;
44 this.Connection = (EntityConnection)value;
49 /// Gets or sets the map connection used by this adapter.
51 public EntityConnection Connection
64 /// Gets or sets whether the IEntityCache.AcceptChanges should be called during a call to IEntityAdapter.Update.
66 public bool AcceptChangesDuringUpdate
70 return this._acceptChangesDuringUpdate;
74 this._acceptChangesDuringUpdate = value;
79 /// Gets of sets the command timeout for update operations. If null, indicates that the default timeout
80 /// for the provider should be used.
82 Int32? IEntityAdapter.CommandTimeout
86 return this._commandTimeout;
90 this._commandTimeout = value;
95 /// Persist modifications described in the given cache.
97 /// <param name="entityCache">Entity cache containing changes to persist to the store.</param>
98 /// <returns>Number of cache entries affected by the udpate.</returns>
99 public Int32 Update(IEntityStateManager entityCache)
101 EntityUtil.CheckArgumentNull(entityCache, "entityCache");
102 if (!IsStateManagerDirty(entityCache)) { return 0; }
104 // Check that we have a connection before we proceed
105 if (_connection == null)
107 throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_NoConnectionForAdapter);
110 // Check that the store connection is available
111 if (_connection.StoreProviderFactory == null || this._connection.StoreConnection == null)
113 throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_NoStoreConnectionForUpdate);
116 // Check that the connection is open before we proceed
117 if (ConnectionState.Open != _connection.State)
119 throw EntityUtil.InvalidOperation(System.Data.Entity.Strings.EntityClient_ClosedConnectionForUpdate);
122 return UpdateTranslator.Update(entityCache, this);
126 /// Determine whether the cache has changes to apply.
128 /// <param name="entityCache">ObjectStateManager to check. Must not be null.</param>
129 /// <returns>true if cache contains changes entries; false otherwise</returns>
130 private static bool IsStateManagerDirty(IEntityStateManager entityCache)
132 Debug.Assert(null != entityCache);
133 bool hasChanges = false;
134 // this call to GetCacheEntries is constant time (the ObjectStateManager implementation
135 // maintains an explicit list of entries in each state)
136 foreach (ObjectStateEntry entry in entityCache.GetEntityStateEntries(
137 EntityState.Added | EntityState.Deleted | EntityState.Modified))