query in LINQ, select with different tables

0

see this method, I repeat the (from Proyectos in db.Proyectos select Proyectos.idProyecto).Max();

I do not like it, I want in var db , get the name of the table, this table previously had already made an insert and in this method I want to bring the last id, which is the one that has been inserted (Point 2) ), I am open to any other idea that can help me improve this code

private long retornaUltimoIdInsertado(string nomTabla)
            {
                long ultimoId = 0;
                var db = new AdminProyectosContext(); 

            if (nomTabla == "Proyecto")
            { 
                ultimoId  =  (from Proyectos in db.Proyectos select Proyectos.idProyecto).Max();
            }
            if (nomTabla == "Actividades")
            {
                ultimoId = (from Actividades in db.Actividades select Actividades.idActividad).Max();
            }

            if (nomTabla == "Bitacora")
            {
                ultimoId = (from Bitacora in db.Bitacora select Bitacora.idBitacora).Max();
            }
            return ultimoId;
        }

Point 2:

  public void TomarValoresIdTablaPrincipal(long[] arrValoIdEliminar, string nomTabla)
        {
            long idFiltrar = 0;
            if (operacion.ToString() ==  estados.Insertar.ToString())
            {
                Proyectos.idProyecto).Max();
                idFiltrar = retornaUltimoIdInsertado(nomTabla);
                BusquedaCamposTabla(idFiltrar);

             }
}

Here everything starts, insertion is done

  [HttpPost]
        [AuthMVCJWT(Roles = "Administrador")]
        public ActionResult AgregarProyecto(Proyectos Proyecto)
        {
            if (Proyecto == null) {
                return new JsonNetResult("Error, no se recibieron datos");
            }

            var db = new AdminProyectosContext();
            var usuario = db.Usuarios.FirstOrDefault(x => x.Username == User.Identity.Name);
            Proyecto.idUsuarioRegistra = usuario.IdUsuario;
            Proyecto.idUsuarioActualiza = usuario.IdUsuario;
            Proyecto.fechaRegistro = DateTime.Now;
            Proyecto.fechaActualizacion = Proyecto.fechaRegistro;
            db.Proyectos.Add(Proyecto);
            db.SaveChanges();

            long[] vacio = new long[]{};
            operacion = estados.Insertar;


            TomarValoresIdTablaPrincipal(vacio,"Proyecto");

            return new JsonNetResult(new { msg = "Guardado exitoso" });
        }

Greetings, anything will be outstanding.

    
asked by Francisco MS380 29.11.2018 в 18:31
source

1 answer

1

Assuming that you are using Entity Framework and the ID of the table is autoincrementado (IDENTITY), you do not need to make a second query (which in passing has a subtle bug) to get the last Id inserted, Entity Framework already returns it to you. In your example:

public ActionResult AgregarProyecto(Proyectos Proyecto)
{
    // ...
    db.Proyectos.Add(Proyecto);
    db.SaveChanges();

    // En este punto, luego de SaveChanges el objeto proyecto ya tiene el nuevo Id

    Proyecto.idProyecto // aquí deberías tener el último Id para que lo uses

    // ...
}
    
answered by 29.11.2018 / 19:41
source