public void Save(Route saveRoute)
        {
            using (BusRoutesContext context = new BusRoutesContext())
            {
                if (saveRoute.Id == 0)
                {
                    context.Routes.Add(saveRoute);
                }
                else
                {
                    //load route with stations
                    var baseRoute = context.Routes.Include(rt => rt.Stations)
                                    .SingleOrDefault(rt => rt.Id == saveRoute.Id);

                    var idsBaseStationNotIntoBaseRoute = saveRoute.Stations.Except(baseRoute.Stations)
                                                         .Where(st => st.Id != 0).Select(st => st.Id).ToList();

                    var baseSavedStationNotIntoRoute = context.Stations.Where(st => idsBaseStationNotIntoBaseRoute.Contains(st.Id)).ToList();

                    //attaching stations saved in database into route
                    foreach (var baseStation in baseSavedStationNotIntoRoute)
                    {
                        baseRoute.Stations.Add(baseStation);
                    }

                    //deleting old stations
                    //can erase new records from other users!!!!!
                    foreach (var station in baseRoute.Stations.Except(saveRoute.Stations).ToList())
                    {
                        baseRoute.Stations.Remove(station);
                    }
                }
                context.SaveChanges();
            }
        }
 public void Update(Station updateStation)
 {
     using (BusRoutesContext context = new BusRoutesContext())
     {
         var stationFromBase = context.Stations
                               .FirstOrDefault(st => st.Id == updateStation.Id);
         updateStation.Save(stationFromBase);
     }
 }
 public RoutesOfTown LoadOneTown()
 {
     using (BusRoutesContext context = new BusRoutesContext())
     {
         var town = context.Towns
                    .Include(tn => tn.Routes.Select(rt => rt.Stations))
                    .FirstOrDefault();
         return(town);
     }
 }
 public void Update(RoutesOfTown updateTown)
 {
     using (BusRoutesContext context = new BusRoutesContext())
     {
         var townFromBase = context.Towns
                            .Include(tn => tn.Routes.Select(rt => rt.Stations))
                            .FirstOrDefault(tn => tn.Id == updateTown.Id);
         updateTown.Save(townFromBase);
     }
 }
 public void Update(Route updateRoute)
 {
     using (BusRoutesContext context = new BusRoutesContext())
     {
         var stationFromBase = context.Routes
                               .Include(rt => rt.Stations)
                               .FirstOrDefault(rt => rt.Id == updateRoute.Id);
         updateRoute.Save(stationFromBase);
     }
 }
        public void Save(Station saveStation)
        {
            try
            {
                using (BusRoutesContext context = new BusRoutesContext())
                {
                    if (saveStation.Id == 0)
                    {
                        context.Stations.Add(saveStation);
                    }
                    else
                    {
                        context.Entry(saveStation).State = System.Data.Entity.EntityState.Modified;
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
        public void Save(RoutesOfTown saveTown)
        {
            using (BusRoutesContext context = new BusRoutesContext())
            {
                if (saveTown.Id == 0)
                {
                    context.Towns.Add(saveTown);
                }
                else
                {
                    //load town with routes
                    var baseTown = context.Towns.Include(tn => tn.Routes)
                                   .SingleOrDefault(tn => tn.Id == saveTown.Id);

                    var idsBaseRoutesNotIntoBaseTown = saveTown.Routes.Except(baseTown.Routes)
                                                       .Where(rt => rt.Id != 0).Select(rt => rt.Id).ToList();

                    var baseSavedRoutesNotIntoTown = context.Routes
                                                     .Where(rt => idsBaseRoutesNotIntoBaseTown.Contains(rt.Id)).ToList();

                    //attaching routes saved in database into town
                    foreach (var baseRoute in baseSavedRoutesNotIntoTown)
                    {
                        baseTown.Routes.Add(baseRoute);
                    }

                    //deleting old routes
                    //can erase new records from other users!!!!!
                    foreach (var route in baseTown.Routes.Except(saveTown.Routes).ToList())
                    {
                        baseTown.Routes.Remove(route);
                    }
                }

                context.SaveChanges();
            }
        }
Ejemplo n.º 8
0
        public static void GenerateTestDataAndSave()
        {
            List <Station> stationsRoute11 = new List <Station>();

            Route route11 = new Route(3452, 11, stationsRoute11.FirstOrDefault(), stationsRoute11.LastOrDefault(), stationsRoute11);

            var beginStation = new Station(235235, 1, "Ivanovo", GetTodayDateTime(9, 45), null, "Ivanovo station. 5 min.", route11);

            stationsRoute11.Add(beginStation);

            stationsRoute11.Add(new Station(945853, 2, "Airport", GetTodayDateTime(9, 50), null, "Airport station. 15 min.", route11));

            stationsRoute11.Add(new Station(124590, 3, "Lake", GetTodayDateTime(10, 10), null, "Lake station. 5 min. Temporarily closed.", route11));

            var endStation = new Station(744935, 4, "Central station.", GetTodayDateTime(10, 30), null, "Central station. 7 min.", route11);

            stationsRoute11.Add(endStation);

            route11.Stations     = stationsRoute11;
            route11.BeginStation = beginStation;
            route11.EndStation   = endStation;



            //List<Station> stationsRoute40 = new List<Station>();

            //stationsRoute40.Add(new Station(495637, 1, "Square", GetTodayDateTime(9, 35), null, "Square station. 7 min."));

            //stationsRoute40.Add(new Station(434588, 2, "BigShop", GetTodayDateTime(10, 50), null, "BigShop station. 5 min."));

            //stationsRoute40.Add(new Station(324589, 3, "Stadium", GetTodayDateTime(11, 10), null, "Stadium station. 5 min."));

            //stationsRoute40.Add(new Station(393428, 4, "River", GetTodayDateTime(12, 30), null, "River station. 11 min."));


            //Route route40 = new Route(9983, 40, stationsRoute40.FirstOrDefault(), stationsRoute40.LastOrDefault(), stationsRoute40);



            //List<Station> stationsRoute52 = new List<Station>();

            //stationsRoute52.Add(new Station(866442, 1, "Forest", GetTodayDateTime(11, 35), GetTodayDateTime(11, 38), "Forest station. 12 min."));

            //stationsRoute52.Add(new Station(124914, 2, "Farm", GetTodayDateTime(12, 38), GetTodayDateTime(12, 36), "Farm station. 4 min."));

            //stationsRoute52.Add(new Station(146593, 3, "Circus", GetTodayDateTime(16, 10), null, "Circus station. 7 min."));

            //stationsRoute52.Add(new Station(103801, 4, "Institute", GetTodayDateTime(18, 30), null, "Institute station. 10 min."));


            //Route route52 = new Route(8875, 52, stationsRoute52.FirstOrDefault(), stationsRoute52.LastOrDefault(), stationsRoute52);



            List <Route> routes = new List <Route>();

            routes.Add(route11);
            //routes.Add(route40);
            //routes.Add(route52);


            RoutesOfTown routesOfTown = new RoutesOfTown("Chester city routes", routes);

            routesOfTown.Id = 33234;

            using (BusRoutesContext context = new BusRoutesContext())
            {
                context.Towns.Add(routesOfTown);
                context.SaveChanges();
            }
        }
 public KeepContextBusRoutesStorage()
 {
     context = new BusRoutesContext();
 }