예제 #1
0
        public bool Update(Race item)
        {
            try
            {
                using var context = new RacingContext();

                context.Attach(item);
                context.Entry(item).State = EntityState.Modified;
                var copyRacePointList = new List <RacePoint>(item.RacePointList);
                var race = context.RaceList.Include(r => r.RacePointList).Where(r => r.RaceId == item.RaceId).FirstOrDefault();

                foreach (var racePoint in copyRacePointList)
                {
                    if (racePoint.RacePointId == 0)
                    {
                        var oldRacePoint = race.RacePointList.Where(p => p.Position == racePoint.Position && p.RacePointId > 0).FirstOrDefault();

                        if (oldRacePoint != null)
                        {
                            context.Entry(oldRacePoint).State = EntityState.Deleted;
                        }

                        context.Entry(racePoint).State = EntityState.Added;
                    }
                    else
                    {
                        context.Entry(racePoint).State = EntityState.Modified;
                    }
                }

                foreach (var racePoint in race.RacePointList.Where(p => p.Position > copyRacePointList.Max(p => p.Position)).ToList())
                {
                    context.Entry(racePoint).State = EntityState.Deleted;
                }

                foreach (var racePart in race.RacePartList)
                {
                    if (racePart.RacePartId == 0)
                    {
                        context.Entry(racePart).State = EntityState.Added;
                    }
                    else
                    {
                        context.Entry(racePart).State = EntityState.Modified;
                    }
                }

                context.SaveChanges();
                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
예제 #2
0
 public bool Update(Team item)
 {
     try
     {
         using var context = new RacingContext();
         context.Attach(item);
         context.Entry(item).State = EntityState.Modified;
         context.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }