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
    public static void InsertWithSpecificID <TEntity>(this TEntity entity) where TEntity : class
    {
        FamusEntities db = new FamusEntities();
        {
            string tableName = db.GetTableNameFromContext <TEntity>();

            List <string> properties = new List <string>();
            List <Tuple <EdmProperty, EdmProperty> > propertyMapping = db.GetEntityMapping(entity);
            List <string> values = new List <string>();
            entity.GetType().GetProperties().Where(x => !x.GetMethod.IsVirtual).ForEach(x =>
            {
                Tuple <EdmProperty, EdmProperty> propertyMap = propertyMapping.Where(y => y.Item2.Name == x.Name).FirstOrDefault();
                if (propertyMap != null)
                {
                    properties.Add(propertyMap.Item1.Name);
                }

                object value = x.GetValue(entity);
                if (value == null)
                {
                    values.Add("NULL");
                }
                else if (value.GetType() == typeof(string))
                {
                    values.Add($"'{value}'");
                }
                //else if (value.GetType() == typeof(char)) values.Add($"'{value}'"); // needs tested
                else if (value.GetType() == typeof(DateTime))
                {
                    values.Add($"'{(DateTime)value:yyyy-MM-dd HH:mm:ss.fff}'");
                }
                else if (value.GetType() == typeof(bool))
                {
                    values.Add($"{Convert.ToInt32(value)}");
                }
                else
                {
                    values.Add($"{value}");
                }
            });
            db.Database.ExecuteSqlCommand(string.Format("SET IDENTITY_INSERT {0} ON INSERT INTO {0} ({1}) VALUES({2}) SET IDENTITY_INSERT {0} OFF", tableName, string.Join(", ", properties), string.Join(", ", values)));
        }
    }
Beispiel #3
0
 /// <summary>
 /// This saves any TEntity object from Entity Framework.
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="entity"></param>
 public static void Save <TEntity>(this TEntity entity) where TEntity : class
 {
     try
     {
         if (entity != null)
         {
             FamusEntities db = new FamusEntities();
             {
                 db.Set <TEntity>().AddOrUpdate(entity);
                 db.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         ThreadPool.QueueUserWorkItem(x =>
         {
             // error logging save here
         });
         throw ex;
     }
 }