Beispiel #1
0
    /// <summary>
    /// This bulk deletes any TEntity object from Entity Framework.
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <param name="entities"></param>
    public static void HardDeleteList <TEntity>(this List <TEntity> entities) where TEntity : class
    {
        // https://stackoverflow.com/questions/5940225/fastest-way-of-inserting-in-entity-framework

        try
        {
            if (entities.Count > 0)
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    FamusEntities context = null;
                    try
                    {
                        context = new FamusEntities();
                        context.Configuration.AutoDetectChangesEnabled = false;
                        //context.Configuration.ValidateOnSaveEnabled = false; still testing

                        int count = 1;
                        foreach (TEntity entity in entities)
                        {
                            context.Set <TEntity>().Attach(entity);
                            context.Set <TEntity>().Remove(entity);

                            if (count % 100 == 0)
                            {
                                context.SaveChanges();
                                context.Dispose();
                                context = new FamusEntities();
                                context.Configuration.AutoDetectChangesEnabled = false;
                                //context.Configuration.ValidateOnSaveEnabled = false; still testing
                            }

                            count++;
                        }

                        context.SaveChanges();
                    }
                    finally { if (context != null)
                              {
                                  context.Dispose();
                              }
                    }

                    scope.Complete();
                }
            }
        }
        catch (Exception ex)
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
                // error logging save here
            });
            throw ex;
        }
    }
Beispiel #2
0
 /// <summary>
 /// This deletes any TEntity object from Entity Framework.
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 public static void HardDelete <TEntity>(this TEntity entity) where TEntity : class
 {
     try
     {
         if (entity != null)
         {
             FamusEntities db = new FamusEntities();
             {
                 db.Set <TEntity>().Attach(entity);
                 db.Set <TEntity>().Remove(entity);
                 db.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         ThreadPool.QueueUserWorkItem(x =>
         {
             // error logging save here
         });
         throw ex;
     }
 }