public AlarmEntity Get(int id) { using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { Entities.AlarmEntity alarm = context.EntityAlarms.FirstOrDefault(a => a.Id == id); if (alarm == null) { throw new AlarmException("Alarm not found."); } return(helper.ToAlarmEntityBL(alarm)); } }
public void Delete(int?id) { if (id == null) { throw new AlarmException("You must select an Alarm to delete."); } using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { Entities.AlarmEntity toRemove = context.EntityAlarms.FirstOrDefault(a => a.Id == id); if (toRemove == null) { throw new AlarmException("Alarm not found."); } context.EntityAlarms.Remove(toRemove); context.SaveChanges(); } }
public void Add(AlarmEntity alarm) { using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { if (context.EntityAlarms.Any(a => a.Id == alarm.Id)) { throw new AlarmException("An Alarm with the same ID already exists."); } ExistsAlarm(alarm.Entity, alarm.NumberDays, alarm.PostQuantity, alarm.Type); alarm.AnalyzePhrases(helper.GetPhrases(context.Phrases)); Entities.AlarmEntity toAdd = helper.ToAlarmEntityEF(alarm); context.EntityAlarms.Add(toAdd); ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toAdd.Entity, toAdd.Entity.Id != alarm.Entity.Id ? EntityState.Modified : EntityState.Unchanged); context.SaveChanges(); alarm.Id = toAdd.Id; } }