示例#1
0
        public async Task Update(string locationId, IEnumerable <ChargePoint> chargePoints)
        {
            var requestIds = chargePoints.Select(x => x.ChargePointId);

            var existingIds = context.ChargePoints
                              .Where(x => x.LocationId == locationId)
                              .Select(x => x.ChargePointId)
                              .ToList();
            var newChargePoints    = chargePoints.Where(x => !existingIds.Contains(x.ChargePointId));
            var newChargePointsIds = newChargePoints.Select(x => x.ChargePointId);

            requestIds = requestIds.Where(x => !newChargePointsIds.Contains(x));

            var idsToDelete = existingIds.Where(x => !requestIds.Contains(x));

            foreach (var id in idsToDelete)
            {
                var chargePoint = new ChargePoint()
                {
                    ChargePointId = id, Status = "Deleted"
                };
                context.Attach(chargePoint);
                context.Entry(chargePoint).Property(x => x.Status).IsModified = true;
            }

            foreach (var chargePoint in chargePoints.Where(x => !idsToDelete.Contains(x.ChargePointId) &&
                                                           !newChargePointsIds.Contains(x.ChargePointId)))
            {
                context.Entry(chargePoint).State = EntityState.Modified;
            }

            await context.AddRangeAsync(newChargePoints);

            await context.SaveChangesAsync();
        }
示例#2
0
        public async Task Update(Location location)
        {
            var currentLocation = await context.Locations.FindAsync(location.LocationId);

            var entry = context.Entry(currentLocation);

            context.Attach(currentLocation);
            foreach (var fromProp in typeof(Location).GetProperties())
            {
                var toProp    = typeof(Location).GetProperty(fromProp.Name);
                var fromValue = fromProp.GetValue(currentLocation, null);
                var toValue   = toProp.GetValue(location, null);
                if (toValue != null && !fromValue.Equals(toValue))
                {
                    fromProp.SetValue(currentLocation, toValue, null);
                    entry.Property(fromProp.Name).IsModified = true;
                }
            }

            await context.SaveChangesAsync();
        }