public RouteDTO Save(RouteDTO route) { RouteDTO savedRoute = null; using (var context = new DeliveryServiceDbContext()) { Point sourceEntity = context.Points.Find(route.OriginId); Point destinationEntity = context.Points.Find(route.DestinationId); if (sourceEntity == null) { throw new ApplicationException("Route source point does not exist."); } if (destinationEntity == null) { throw new ApplicationException("Route destination point does not exist."); } Route routeEntity = new Route() { Id = route.Id, Origin = sourceEntity, Destination = destinationEntity, Cost = route.Cost, Minutes = route.Minutes }; if (routeEntity.Id == 0) { context.Entry(routeEntity).State = EntityState.Added; } else { context.Entry(routeEntity).State = EntityState.Modified; } context.SaveChanges(); savedRoute = typeMapper.Map <Route, RouteDTO>(routeEntity); } return(savedRoute); }
public void RollBack() { var changedEntriesCopy = _context.ChangeTracker.Entries() .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted).ToList(); foreach (var entity in changedEntriesCopy) { _context.Entry(entity.Entity).State = EntityState.Detached; } }
public PointDTO Save(PointDTO point) { Point pointEntity = typeMapper.Map <PointDTO, Point>(point); using (var context = new DeliveryServiceDbContext()) { if (pointEntity.Id == 0) { context.Entry(pointEntity).State = EntityState.Added; } else { context.Entry(pointEntity).State = EntityState.Modified; } context.SaveChanges(); } return(typeMapper.Map <Point, PointDTO>(pointEntity)); }
public void Delete(int pointId) { using (var context = new DeliveryServiceDbContext()) { Point pointEntity = context.Points.Find(pointId); if (pointEntity != null) { context.Entry <Point>(pointEntity).State = EntityState.Deleted; context.SaveChanges(); } else { throw new ArgumentNullException("pointId", "The referenced point does not exist."); } } }
public void Delete(int routeId) { using (var context = new DeliveryServiceDbContext()) { Route routeEntity = context.Routes.Find(routeId); if (routeEntity != null) { context.Entry <Route>(routeEntity).State = EntityState.Deleted; context.SaveChanges(); } else { throw new ArgumentNullException("routeId", "The referenced point does not exist."); } } }
public virtual void Update(D obj) { _context.Entry(obj).State = EntityState.Modified; _context.Entry(obj); }