Пример #1
0
        public void CheckUnfinishedEntries()
        {
            using (var context = new AppsEntities())
            {
                var unfinishedUsages = context.Usages
                                       .Where(u => u.IsCurrent && u.UsageType == UsageTypes.Login)
                                       .AsNoTracking()
                                       .ToList();
                var unfinishedLogs = context.Logs
                                     .Where(l => !l.Finished)
                                     .AsNoTracking()
                                     .ToList();

                if (unfinishedUsages.Count > 0)
                {
                    foreach (var usage in unfinishedUsages)
                    {
                        SaveUnfinishedUsage(usage);
                    }
                }

                if (unfinishedLogs.Count > 0)
                {
                    unfinishedLogs.ForEach(l =>
                    {
                        l.Finished             = true;
                        context.Entry(l).State = EntityState.Modified;
                    });
                    context.SaveChanges();
                }
            }
        }
Пример #2
0
 public void SaveNewEntityRange <T>(IEnumerable <T> items) where T : class
 {
     using (var context = new AppsEntities())
     {
         context.Set <T>().AddRange(items);
         context.SaveChanges();
     }
 }
Пример #3
0
 public void SaveNewEntity <T>(T item) where T : class
 {
     using (var context = new AppsEntities())
     {
         context.Set <T>().Add(item);
         context.SaveChanges();
     }
 }
Пример #4
0
 public void SaveModifiedEntity <T>(T item) where T : class
 {
     using (var context = new AppsEntities())
     {
         context.Entry <T>(item).State = EntityState.Modified;
         context.SaveChanges();
     }
 }
Пример #5
0
 public void DeleteByIds <T>(IEnumerable <Int32> ids) where T : class, IEntity
 {
     using (var context = new AppsEntities())
     {
         var items = context.Set <T>().Where(e => ids.Contains(e.ID));
         context.Set <T>().RemoveRange(items);
         context.SaveChanges();
     }
 }
Пример #6
0
 public void DeleteEntityRange <T>(IEnumerable <T> range) where T : class
 {
     using (var context = new AppsEntities())
     {
         foreach (var item in range)
         {
             context.Entry <T>(item).State = EntityState.Deleted;
         }
         context.SaveChanges();
     }
 }
Пример #7
0
 public void SaveModifiedEntityRange <T>(IEnumerable <T> items) where T : class
 {
     using (var context = new AppsEntities())
     {
         foreach (var item in items)
         {
             context.Entry(item).State = EntityState.Modified;
         }
         context.SaveChanges();
     }
 }
Пример #8
0
        private void SaveUnfinishedUsage(Usage usage)
        {
            using (var context = new AppsEntities())
            {
                var lastLog = context.Logs.Where(l => l.UsageID == usage.UsageID)
                              .OrderByDescending(l => l.DateCreated)
                              .FirstOrDefault();
                var lastUsage = context.Usages.Where(u => u.SelfUsageID == usage.UsageID)
                                .OrderByDescending(u => u.UsageEnd)
                                .FirstOrDefault();

                var lastLogDate   = DateTime.MinValue;
                var lastUsageDate = DateTime.MinValue;

                if (lastLog != null)
                {
                    lastLogDate = lastLog.DateEnded;
                }

                if (lastUsage != null)
                {
                    lastUsageDate = lastUsage.UsageEnd;
                }

                var latestDateKnown = new DateTime[] { lastLogDate, lastUsageDate }
                .OrderByDescending(d => d.Ticks)
                .FirstOrDefault();

                if (latestDateKnown == DateTime.MinValue)
                {
                    latestDateKnown = usage.UsageStart;
                }

                usage.UsageEnd  = latestDateKnown;
                usage.IsCurrent = false;

                context.Entry(usage).State = EntityState.Modified;
                context.SaveChanges();
            }
        }