public bool UpdatePoliceman(Policeman policeman) { try { var updatePoliceman = mapper.Map <CodeFirstModels.Policeman>(policeman); using (var context = new KatDataContext()) { //var dbRecord = context.Policemеn.FirstOrDefault(v => v.Id == policeman.Id); //PropertyCopy.Copy(updatePoliceman, dbRecord); var docInDb = context.Policemеn.Find(updatePoliceman.Id); if (docInDb == null) { context.Policemеn.Add(updatePoliceman); context.SaveChanges(); return(true); } if (updatePoliceman.Rank != null) { context.Ranks.Attach(updatePoliceman.Rank); docInDb.Rank = updatePoliceman.Rank; } context.Entry(docInDb).CurrentValues.SetValues(updatePoliceman); context.Entry(docInDb).State = EntityState.Modified; context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public bool DeleteDocument(long id) { using (var context = new KatDataContext()) { var delete = context.Documents.FirstOrDefault(d => d.Id == id); if (delete != null) { context.Documents.Remove(delete); context.SaveChanges(); return(true); } return(false); } }
public bool DeleteCar(long id) { try { using (var context = new KatDataContext()) { var removeCar = context.Cars.FirstOrDefault(c => c.Id == id); context.Cars.Remove(removeCar); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public long AddCar(Car car) { try { var insertCar = Mapper.Map <CodeFirstModels.Car>(car); using (var context = new KatDataContext()) { context.Cars.Add(insertCar); context.SaveChanges(); } return(insertCar.Id); } catch (Exception) { return(0); } }
public bool DeleteViolation(long id) { try { using (var context = new KatDataContext()) { var removeViolation = context.Violations.FirstOrDefault(v => v.Id == id); context.Violations.Remove(removeViolation); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public bool AddViolation(Violation violation) { try { var insertViolation = mapper.Map <CodeFirstModels.Nomenclatures.Violation>(violation); using (var context = new KatDataContext()) { context.Violations.Add(insertViolation); context.SaveChanges(); } return(true); } catch (Exception e) { return(false); } }
public bool AddPoliceman(Policeman policeman) { try { var insertPoliceman = mapper.Map <CodeFirstModels.Policeman>(policeman); using (var context = new KatDataContext()) { context.Policemеn.Add(insertPoliceman); context.SaveChanges(); } return(true); } catch (Exception e) { return(false); } }
public bool AddCamera(Camera camera) { try { var insertCamera = mapper.Map <CodeFirstModels.Nomenclatures.Camera>(camera); using (var context = new KatDataContext()) { context.Cameras.Add(insertCamera); context.SaveChanges(); } return(true); } catch (Exception e) { return(false); } }
public long AddDriver(Driver driver) { try { var dbDriver = Mapper.Map <CodeFirstModels.Driver>(driver); using (var context = new KatDataContext()) { context.Drivers.Add(dbDriver); context.SaveChanges(); } return(dbDriver.Id); } catch (Exception) { return(0); } }
public bool UpdateCar(Car car) { try { var updateCar = Mapper.Map <CodeFirstModels.Car>(car); using (var context = new KatDataContext()) { var dbRecord = context.Cars.FirstOrDefault(d => d.Id == car.Id); PropertyCopy.Copy(updateCar, dbRecord); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public bool UpdateViolation(Violation violation) { try { var updateViolation = mapper.Map <CodeFirstModels.Nomenclatures.Violation>(violation); using (var context = new KatDataContext()) { var dbRecord = context.Violations.FirstOrDefault(v => v.Id == violation.Id); PropertyCopy.Copy(updateViolation, dbRecord); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public bool UpdateCamera(Camera camera) { try { var updateCamera = mapper.Map <CodeFirstModels.Nomenclatures.Camera>(camera); using (var context = new KatDataContext()) { var dbRecord = context.Cameras.FirstOrDefault(v => v.Id == camera.Id); PropertyCopy.Copy(updateCamera, dbRecord); context.SaveChanges(); } return(true); } catch (Exception) { return(false); } }
public bool UpdateDocument(Document document) { try { var updateDocument = mapper.Map <CodeFirstModels.Document>(document); using (var context = new KatDataContext()) { var result = context.Documents.Where(d => d.RegNumber == updateDocument.RegNumber).ToList(); if (result.Count != 1) { return(false); } var recordToUpdate = result.FirstOrDefault(); // Attach props: if (updateDocument.Driver != null) { // context.Cars.ToList().FindAll(c => c.Driver.Id == updateDocument.Driver.Id).ToList() updateDocument.Driver.Cars = null; context.Drivers.Attach(updateDocument.Driver); recordToUpdate.Driver = updateDocument.Driver; } if (updateDocument.Violations != null && updateDocument.Violations.Any()) { updateDocument.Violations.ToList().ForEach(v => context.Violations.Attach(v)); recordToUpdate.Violations = new List <CodeFirstModels.Nomenclatures.Violation>(); updateDocument.Violations.ToList().ForEach(v => recordToUpdate.Violations.Add(v)); } if (updateDocument.Camera != null) { context.Cameras.Attach(updateDocument.Camera); recordToUpdate.Camera = updateDocument.Camera; } if (updateDocument.Policeman != null) { context.Policemеn.Attach(updateDocument.Policeman); recordToUpdate.Policeman = updateDocument.Policeman; } if (updateDocument.DocumentType != null) { context.DocumentTypes.Attach(updateDocument.DocumentType); recordToUpdate.DocumentType = updateDocument.DocumentType; } if (updateDocument.Picture != null) { recordToUpdate.Picture = updateDocument.Picture; } recordToUpdate.Id = updateDocument.Id; recordToUpdate.RegNumber = updateDocument.RegNumber; //var docInDb = context.Documents.Find(updateDocument.Id); //// Activity does not exist in database and it's new one //if (docInDb == null) //{ // context.Documents.Add(updateDocument); // context.SaveChanges(); // return true; //} //// Activity already exist in database and modify it //context.Entry(docInDb).CurrentValues.SetValues(updateDocument); //context.Entry(docInDb).State = EntityState.Modified; context.SaveChanges(); return(true); } } catch (Exception e) { return(false); } }
public long InsertDocument(Document document) { try { var insertDocument = mapper.Map <CodeFirstModels.Document>(document); using (var context = new KatDataContext()) { var newDoc = new CodeFirstModels.Document { Date = insertDocument.Date, Picture = insertDocument.Picture, RegNumber = insertDocument.RegNumber }; context.Documents.Add(newDoc); // Attach props: if (insertDocument.Driver != null) { insertDocument.Driver.Cars = null; context.Drivers.Attach(insertDocument.Driver); newDoc.Driver = insertDocument.Driver; } if (insertDocument.Violations != null && insertDocument.Violations.Any()) { insertDocument.Violations.ToList().ForEach(v => context.Violations.Attach(v)); newDoc.Violations = insertDocument.Violations; } if (insertDocument.Camera != null) { context.Cameras.Attach(insertDocument.Camera); newDoc.Camera = insertDocument.Camera; } if (insertDocument.Policeman != null) { context.Policemеn.Attach(insertDocument.Policeman); newDoc.Policeman = insertDocument.Policeman; } if (insertDocument.DocumentType != null) { context.DocumentTypes.Attach(insertDocument.DocumentType); newDoc.DocumentType = insertDocument.DocumentType; } //var docInDb = context.Documents.Find(insertDocument.Id); //if (docInDb == null) //{ // insertDocument.Driver.Cars.ToList().ForEach(c => c.Driver = null); // context.Documents.Add(insertDocument); // context.SaveChanges(); // return insertDocument.Id; //} //context.Entry(docInDb).CurrentValues.SetValues(insertDocument); //context.Entry(docInDb).State = EntityState.Modified; context.SaveChanges(); return(newDoc.Id); } } catch (Exception e) { return(0); } }