public override void AddOrUpdate(TachographMake entity) { Safely(() => { using (var context = new TachographContext()) { TachographMake existingMake = context.Set <TachographMake>().Find(entity.Id); if (existingMake != null) { context.Entry(existingMake).CurrentValues.SetValues(entity); foreach (var tachographModel in entity.Models) { if (tachographModel.IsNewEntity) { if (!string.IsNullOrEmpty(tachographModel.Name)) { tachographModel.TachographMake = existingMake; context.Entry(tachographModel).State = EntityState.Added; } } } } else { context.Set <TachographMake>().Add(entity); } context.SaveChanges(); } }); }
public virtual T First(Expression <Func <T, bool> > predicate) { return(Safely(() => { using (var context = new TachographContext()) { return context.Set <T>().First(predicate.Compile()); } })); }
public bool Any(Expression <Func <T, bool> > predicate) { return(Safely(() => { using (var context = new TachographContext()) { return context.Set <T>().Any(predicate); } })); }
public T First() { return(Safely(() => { using (var context = new TachographContext()) { return context.Set <T>().First(); } })); }
public bool Any() { return(Safely(() => { using (var context = new TachographContext()) { return context.Set <T>().Any(); } })); }
public virtual void Save(T settings) { Safely(() => { using (var context = new TachographContext()) { if (settings == null) { context.Set <T>().Add(new T()); } else { context.Set <T>().Attach(settings); context.Entry(settings).State = EntityState.Modified; } context.SaveChanges(); } }); }
public virtual void Add(T entity) { Safely(() => { using (var context = new TachographContext()) { context.Set <T>().Add(entity); context.SaveChanges(); } }); }
public virtual void AddOrUpdate(T entity) { Safely(() => { using (var context = new TachographContext()) { T existing = context.Set <T>().Find(entity.Id); if (existing != null) { context.Entry(existing).CurrentValues.SetValues(entity); } else { context.Set <T>().Add(entity); } context.SaveChanges(); } }); }
public virtual ICollection <T> GetAll(bool includeDeleted, params string[] includes) { return(Safely(() => { using (var context = new TachographContext()) { var query = context.Set <T>().WithIncludes(context, includes); if (!includeDeleted) { query = query.Where(c => c.Deleted == null); } return query.ToList(); } })); }
public ICollection <T> Where(Expression <Func <T, bool> > predicate, bool includeDeleted) { return(Safely(() => { using (var context = new TachographContext()) { var query = context.Set <T>().Where(predicate.Compile()); if (!includeDeleted) { query = query.Where(c => c.Deleted == null); } return query.ToList(); } })); }
public override void AddOrUpdate(TachographDocument entity) { Safely(() => { using (var context = new TachographContext()) { TachographDocument existing = context.TachographDocuments.Find(entity.Id); if (existing != null) { context.Entry(existing).CurrentValues.SetValues(entity); } else { context.Set <TachographDocument>().Add(entity); } CheckVehicleExists(context, entity); context.SaveChanges(); } }); }
public T Get(Func <T, bool> filter, params string[] includes) { List <T> settings = Safely(() => { using (var context = new TachographContext()) { return(context.Set <T>().WithIncludes(context, includes).ToList()); } }); if (settings.Count == 1) { return(settings.First()); } if (filter != null) { return(settings.FirstOrDefault(filter)); } return(settings.FirstOrDefault()); }
public override void Save(WorkshopSettings settings) { Safely(() => { using (var context = new TachographContext()) { var originalEntity = context.WorkshopSettings.AsNoTracking().FirstOrDefault(c => c.Id == settings.Id); if (originalEntity != null) { if (!originalEntity.Equals(settings)) { settings.Uploaded = null; } } //Bin old data, don't need it context.CustomDayOfWeeks.RemoveRange(context.CustomDayOfWeeks); context.SaveChanges(); var daysOfWeek = settings.CustomDayOfWeeks.Clone(); //Save settings settings.CustomDayOfWeeks = null; context.Set <WorkshopSettings>().Attach(settings); context.Entry(settings).State = EntityState.Modified; context.SaveChanges(); //Save days of week daysOfWeek.ForEach(c => c.WorkshopSettings = settings); context.CustomDayOfWeeks.AddRange(daysOfWeek); context.SaveChanges(); settings.CustomDayOfWeeks = context.CustomDayOfWeeks.ToList(); } }); }
public override ICollection <TachographMake> GetAll(bool includeDeleted, params string[] includes) { return(Safely(() => { using (var context = new TachographContext()) { var query = context.Set <TachographMake>().WithIncludes(context, includes).Where(c => c.Name != null); if (!includeDeleted) { query = query.Where(c => c.Deleted == null); } query = query.OrderBy(c => c.Name); List <TachographMake> items = query.ToList(); foreach (TachographMake item in items) { item.Models = new ObservableCollection <TachographModel>(item.Models.Where(m => m.Name != null).OrderBy(c => c.Name)); if (!includeDeleted) { item.Models = new ObservableCollection <TachographModel>(item.Models.Where(c => c.Deleted == null)); } if (item.Models.All(m => m.Name != null)) { item.Models.Insert(0, new TachographModel()); } } items.Insert(0, null); return items; } })); }
public static IEnumerable <T> GetReports <T>(this TachographContext context) where T : BaseModel { return(from document in context.Set <T>() where document.Deleted == null select document); }
public static IEnumerable <T> GetDocuments <T>(this TachographContext context) where T : Document { return(from document in context.Set <T>().SqlQuery(FastQueryHelper.GetSqlQueryFor <T>(false)) where document.Deleted == null select document); }