예제 #1
0
        public async Task Update(Avance model)
        {
            try
            {
                var _model = await _db.DbSetAvance.FirstOrDefaultAsync(e => e.AvanceId == model.AvanceId);

                if (_model != null)
                {
                    if (model.AdjuntoId != null)
                    {
                        int id = Convert.ToInt32(model.AdjuntoId);
                        model.AdjuntoId = null;
                        _db.Entry(_model).CurrentValues.SetValues(model);
                        await _db.SaveChangesAsync();

                        await _adjuntoRepo.Delete(id);
                    }
                    if (model.Adjunto != null && model.AdjuntoId == null)
                    {
                        Adjunto key = await _adjuntoRepo.CreateAd(model.Adjunto);

                        model.AdjuntoId         = key.AdjuntoId;
                        model.Adjunto.AdjuntoId = key.AdjuntoId;
                    }
                    if (model.avances != null)
                    {
                        //Se eliminan los anteriores para crear un nuevo registro de los agregados
                        var Autores = await _db.DbSetAvanceMiembros.Where(e => e.AvanceId == model.AvanceId).
                                      Select(x => x.AvanceMiembroId).ToListAsync();

                        AvanceMiembrosRepository av = new AvanceMiembrosRepository();
                        foreach (var aut in Autores)
                        {
                            await av.Delete(aut);
                        }

                        //Ahora se crean los nuevos autores
                        foreach (var c in model.avances)
                        {
                            c.AvanceId      = model.AvanceId;
                            c.FechaRegistro = DateTime.Now;;
                            await av.Create(c);
                        }
                    }

                    _db.Entry(_model).CurrentValues.SetValues(model);
                    await _db.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message, e);
            }
        }