Example #1
0
        public Candidato save(int id = 0)
        {
            try
            {
                EasyDevContext context = new EasyDevContext();

                if (this.id == 0 && id == 0)
                {
                    this.dtHoraCadastro = DateTime.Now;

                    var conhecimentos = new List <CandidatoConhecimento>(this.conhecimentos);
                    this.conhecimentos = null;

                    context.Candidatos.Add(this);

                    context.SaveChanges();

                    CandidatoConhecimento.save(this.id, conhecimentos);
                }
                else
                {
                    if (this.id != id)
                    {
                        throw new HttpException(404, "id do registro difere do id passado como parâmetro");
                    }

                    Candidato oldObj = context.Candidatos.Find(this.id);
                    if (oldObj == null)
                    {
                        throw new HttpException(404, "Registro não localizado.");
                    }
                    else
                    {
                        context.Entry(oldObj).CurrentValues.SetValues(this);
                    }

                    foreach (var candidatoConhecimento in this.conhecimentos)
                    {
                        candidatoConhecimento.save(candidatoConhecimento.id);
                    }

                    context.SaveChanges();
                }

                return(this);
            }
            catch (Exception)
            {
                throw;
            }
        }