internal static int Update(IExtensibleEntityCache cache, IEntityUpdateAdapter adapter, DbTransaction storeTransaction) { int objectsAffected = 0; try { //TODO: To support other type of database IUpdateScriptGenerator scriptGenenrator = new SqlUpdateScriptGenerator(); IEnumerable <EntityCacheEntry> dirtyEntries = cache.GetCacheEntries(EntityRowState.Added | EntityRowState.Modified | EntityRowState.Deleted); List <string> scripts = new List <string>(); foreach (EntityCacheEntry entry in dirtyEntries) { scripts.AddRange(scriptGenenrator.GenerateUpdateScripts(entry)); } foreach (string script in scripts) { using (DbCommand cmd = storeTransaction.Connection.CreateCommand()) { Trace.WriteLine(script); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = script; cmd.Transaction = storeTransaction; cmd.ExecuteNonQuery(); } } objectsAffected += AcceptChanges(dirtyEntries); } catch (DbException exception) { throw new UpdateException("Exception when update the entity cache", exception); } return(objectsAffected); }
private static bool IsCacheDirty(IExtensibleEntityCache entityCache) { bool flag = false; using (IEnumerator <EntityCacheEntry> enumerator = entityCache.GetCacheEntries(EntityRowState.Modified | EntityRowState.Deleted | EntityRowState.Added).GetEnumerator()) { while (enumerator.MoveNext()) { EntityCacheEntry entry = enumerator.Current; return(true); } } return(flag); }
/// <summary> /// Update the entity cache to database /// </summary> /// <param name="entityCache">The cache object</param> /// <returns>Rows affected</returns> public int Update(IExtensibleEntityCache entityCache) { if (entityCache == null) { throw new ArgumentNullException("entityCache"); } //ADP.CheckArgumentNull<IEntityCache>(entityCache, "entityCache"); if (!EntityUpdateAdapter.IsCacheDirty(entityCache)) { return(0); } if (this._connection == null) { throw new InvalidOperationException("No connection for adapter"); } bool flag = ConnectionState.Open != this._connection.State; if (flag) { this._connection.Open(); } using (DbTransaction transaction = this._connection.BeginTransaction()) { try { int affectedRows = EntityCacheUpdateTranslator.Update(entityCache, this, transaction); transaction.Commit(); return(affectedRows); } catch { transaction.Rollback(); throw; } finally { if (flag && (this._connection.State != ConnectionState.Closed)) { this._connection.Close(); } } } }