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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Save all changes to database
 /// </summary>
 public void SaveAllChanges()
 {
     this.CheckConnection();
     if (this._adapter == null)
     {
         //TODO: the adapter should be configurable
         this._adapter = new EntityUpdateAdapter();
     }
     this.SaveChanges(this._adapter);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Save all changes to database by a adapter
 /// </summary>
 /// <param name="adapter"></param>
 protected virtual void SaveChanges(IEntityUpdateAdapter adapter)
 {
     if (adapter == null)
     {
         throw new ArgumentNullException("adapter");
     }
     this.CheckConnection();
     base.OpenConnection();
     adapter.Connection = base.Connection;
     adapter.Update(base.Cache);
 }