Пример #1
0
 public override int SaveChanges(SaveOptions options)
 {
     int returnValue = 0;
     // 因为我们不调用base.SaveChanges, 我们必须手动关闭链接.
     // 否则我们将留下许多打开的链接, 最终导致链接瓶颈.
     // Entity Framework提供了base.SaveChanges内部使用的EnsureConnection和ReleaseConnection.
     // 这些是内部方法, 所以我们必须使用反射调用它们.
     var EnsureConnectionMethod = typeof(ObjectContext).GetMethod(
         "EnsureConnection", BindingFlags.Instance | BindingFlags.NonPublic);
     EnsureConnectionMethod.Invoke(this, null);
     // 使用ObjectStateManager.GetObjectStateEntries完成增加,修改,和删除集合.
     foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
     {
         Travel travel = ose.Entity as Travel;
         if (travel != null)
         {
             RetryPolicy retryPolicy = new RetryPolicy();
             retryPolicy.Task = new Action(() =>
             {
                 this.InsertIntoTravel(travel.PartitionKey,
                     travel.Place, travel.GeoLocationText, travel.Time);
             });
             retryPolicy.Execute();
             returnValue++;
         }
     }
     foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
     {
         Travel travel = ose.Entity as Travel;
         if (travel != null)
         {
             RetryPolicy retryPolicy = new RetryPolicy();
             retryPolicy.Task = new Action(() =>
             {
                 this.UpdateTravel(travel.PartitionKey,
                     travel.RowKey, travel.Place, travel.GeoLocationText, travel.Time);
             });
             retryPolicy.Execute();
             returnValue++;
         }
     }
     foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
     {
         Travel travel = ose.Entity as Travel;
         if (travel != null)
         {
             RetryPolicy retryPolicy = new RetryPolicy();
             retryPolicy.Task = new Action(() =>
             {
                 this.DeleteFromTravel(travel.PartitionKey, travel.RowKey);
             });
             retryPolicy.Execute();
             returnValue++;
         }
     }
     var ReleaseConnectionMethod = typeof(ObjectContext).
         GetMethod("ReleaseConnection", BindingFlags.Instance | BindingFlags.NonPublic);
     ReleaseConnectionMethod.Invoke(this, null);
     return returnValue;
 }
Пример #2
0
        public override int SaveChanges(SaveOptions options)
        {
            int returnValue = 0;
            // 因为我们不调用base.SaveChanges, 我们必须手动关闭链接.
            // 否则我们将留下许多打开的链接, 最终导致链接瓶颈.
            // Entity Framework提供了base.SaveChanges内部使用的EnsureConnection和ReleaseConnection.
            // 这些是内部方法, 所以我们必须使用反射调用它们.
            var EnsureConnectionMethod = typeof(ObjectContext).GetMethod(
                "EnsureConnection", BindingFlags.Instance | BindingFlags.NonPublic);

            EnsureConnectionMethod.Invoke(this, null);
            // 使用ObjectStateManager.GetObjectStateEntries完成增加,修改,和删除集合.
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.InsertIntoTravel(travel.PartitionKey,
                                              travel.Place, travel.GeoLocationText, travel.Time);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.UpdateTravel(travel.PartitionKey,
                                          travel.RowKey, travel.Place, travel.GeoLocationText, travel.Time);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.DeleteFromTravel(travel.PartitionKey, travel.RowKey);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            var ReleaseConnectionMethod = typeof(ObjectContext).
                                          GetMethod("ReleaseConnection", BindingFlags.Instance | BindingFlags.NonPublic);

            ReleaseConnectionMethod.Invoke(this, null);
            return(returnValue);
        }
        public override int SaveChanges(SaveOptions options)
        {
            int returnValue = 0;
            // Since we do not call base.SaveChanges, we have to take care of connections manually.
            // Otherwise we may leave a lot of connections open, which results in connection throttle.
            // Entity Framework provides EnsureConnection and ReleaseConnection which base.SaveChanges uses internally.
            // They're internal methods, so we have to use reflection to invoke them.
            var EnsureConnectionMethod = typeof(ObjectContext).GetMethod(
                "EnsureConnection", BindingFlags.Instance | BindingFlags.NonPublic);

            EnsureConnectionMethod.Invoke(this, null);
            // Use ObjectStateManager.GetObjectStateEntries to obtain added, modified, and deleted entities.
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Added))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.InsertIntoTravel(travel.PartitionKey,
                                              travel.Place, travel.GeoLocationText, travel.Time);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Modified))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.UpdateTravel(travel.PartitionKey,
                                          travel.RowKey, travel.Place, travel.GeoLocationText, travel.Time);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            foreach (ObjectStateEntry ose in this.ObjectStateManager.GetObjectStateEntries(EntityState.Deleted))
            {
                Travel travel = ose.Entity as Travel;
                if (travel != null)
                {
                    RetryPolicy retryPolicy = new RetryPolicy();
                    retryPolicy.Task = new Action(() =>
                    {
                        this.DeleteFromTravel(travel.PartitionKey, travel.RowKey);
                    });
                    retryPolicy.Execute();
                    returnValue++;
                }
            }
            var ReleaseConnectionMethod = typeof(ObjectContext).
                                          GetMethod("ReleaseConnection", BindingFlags.Instance | BindingFlags.NonPublic);

            ReleaseConnectionMethod.Invoke(this, null);
            return(returnValue);
        }