public ActionResult Edit(int?id)
        {
            IServiceAsegurador _ServiceAsegurador = new ServiceAsegurador();
            Asegurador         asegurador         = null;

            try
            {
                // Si va null
                if (id == null)
                {
                    return(RedirectToAction("List"));
                }

                asegurador = _ServiceAsegurador.GetAseguradorByID(id.Value);
                // Response.StatusCode = 500;
                return(View(asegurador));
            }
            catch (Exception ex)
            {
                // Salvar el error en un archivo
                Log.Error(ex, MethodBase.GetCurrentMethod());
                TempData["Message"] = "Error al procesar los datos! " + ex.Message;
                TempData.Keep();
                // Redireccion a la captura del Error
                return(RedirectToAction("Default", "Error"));
            }
        }
        public Asegurador GetAseguradorByID(int id)
        {
            Asegurador asegurador = null;

            try
            {
                using (MyContext ctx = new MyContext())
                {
                    ctx.Configuration.LazyLoadingEnabled = false;
                    asegurador = ctx.Asegurador.Find(id);
                }

                return(asegurador);
            }
            catch (DbUpdateException dbEx)
            {
                string mensaje = "";
                Log.Error(dbEx, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw new Exception(mensaje);
            }
            catch (Exception ex)
            {
                string mensaje = "";
                Log.Error(ex, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw;
            }
        }
        public void DeleteAsegurador(int id)
        {
            int returno;

            try
            {
                using (MyContext ctx = new MyContext())
                {
                    ctx.Configuration.LazyLoadingEnabled = false;
                    Asegurador asegurador = new Asegurador()
                    {
                        AseguradorID = id
                    };
                    ctx.Entry(asegurador).State = EntityState.Deleted;
                    returno = ctx.SaveChanges();
                }
            }
            catch (DbUpdateException dbEx)
            {
                string mensaje = "";
                Log.Error(dbEx, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw new Exception(mensaje);
            }
            catch (Exception ex)
            {
                string mensaje = "";
                Log.Error(ex, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw;
            }
        }
        public Asegurador Save(Asegurador asegurador)
        {
            int        retorno     = 0;
            Asegurador oAsegurador = null;

            try
            {
                using (MyContext ctx = new MyContext())
                {
                    ctx.Configuration.LazyLoadingEnabled = false;
                    oAsegurador = GetAseguradorByID(asegurador.AseguradorID);
                    if (oAsegurador == null)
                    {
                        ctx.Asegurador.Add(asegurador);
                    }
                    else
                    {
                        ctx.Entry(asegurador).State = EntityState.Modified;
                    }
                    retorno = ctx.SaveChanges();


#if !DEBUG
                    // Error por exception
                    int x = 0;
                    x = 25 / x;

                    // Error por Entity Framework
                    // Forzar un error por duplicidad
                    // bodega.IdBodega = 1;
                    // ctx.Bodega.Add(bodega);
                    // retorno = ctx.SaveChanges();
#endif
                }

                if (retorno >= 0)
                {
                    oAsegurador = GetAseguradorByID(asegurador.AseguradorID);
                }

                return(oAsegurador);
            }
            catch (DbUpdateException dbEx)
            {
                string mensaje = "";
                Log.Error(dbEx, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw new Exception(mensaje);
            }
            catch (Exception ex)
            {
                string mensaje = "";
                Log.Error(ex, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw;
            }
        }
Ejemplo n.º 5
0
        public Asegurador Save(Asegurador asegurador)
        {
            int        retorno     = 0;
            Asegurador oAsegurador = null;

            try
            {
                using (MyContext ctx = new MyContext())
                {
                    ctx.Configuration.LazyLoadingEnabled = false;
                    oAsegurador = GetAseguradorByID(asegurador.IdAsegurador);
                    if (oAsegurador == null)
                    {
                        ctx.Asegurador.Add(asegurador);
                    }
                    else
                    {
                        ctx.Entry(asegurador).State = EntityState.Modified;
                    }
                    retorno = ctx.SaveChanges();
                }

                if (retorno >= 0)
                {
                    oAsegurador = GetAseguradorByID(asegurador.IdAsegurador);
                }

                return(oAsegurador);
            }
            catch (DbUpdateException dbEx)
            {
                string mensaje = "";
                Log.Error(dbEx, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw new Exception(mensaje);
            }
            catch (Exception ex)
            {
                string mensaje = "";
                Log.Error(ex, System.Reflection.MethodBase.GetCurrentMethod(), ref mensaje);
                throw;
            }
        }
        public ActionResult Save(Asegurador asegurador)
        {
            string errores = "";

            try
            {
                // Es valido
                if (ModelState.IsValid)
                {
                    ServiceAsegurador _ServiceAsegurador = new ServiceAsegurador();
                    _ServiceAsegurador.Save(asegurador);
                }
                else
                {
                    // Valida Errores si Javascript está deshabilitado
                    //Util.ValidateErrors(this);

                    TempData["Message"] = "Error al procesar los datos! " + errores;
                    TempData.Keep();

                    return(View("Create", asegurador));
                }

                // redirigir
                return(RedirectToAction("List"));
            }
            catch (Exception ex)
            {
                // Salvar el error en un archivo
                Log.Error(ex, MethodBase.GetCurrentMethod());
                TempData["Message"] = "Error al procesar los datos! " + ex.Message;
                TempData.Keep();
                // Redireccion a la captura del Error
                return(RedirectToAction("Default", "Error"));
            }
        }
        public Asegurador Save(Asegurador asegurador)
        {
            RepositoryAsegurador repository = new RepositoryAsegurador();

            return(repository.Save(asegurador));
        }