Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

307
Vistas
EntityFramework with WEB API, update all properties

I'm using EF with WEB API. I have a PUT Method which updates a entity which already is in the db. Right now I have this:

        // PUT api/fleet/5
        public void Put(Fleet fleet)
        {
            Fleet dbFleet = db.Fleets.Find(fleet.FleetId);
            dbFleet.Name = fleet.Name;
            dbFleet.xy= fleet.xy;
            //and so on....
            db.SaveChanges();
        }

But I'm lazy and would just like to write something like:

dbFleet.update(fleet);

So I don't have to update every property by its own.

I'm sure there is a way but I could only find answers on how to do this with MVC but not when using a WEB API and not receiving the model state.

Thanks

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

db.Fleets.Attach(fleet);
db.Entry(fleet).State = EntityState.Modified;
db.SaveChanges();
over 4 years ago · Santiago Trujillo Denunciar

0

Just found the answer...

// PUT api/fleet/5
public void Put(Fleet fleet)
{
    db.Entry(fleet).State = EntityState.Modified;
    db.SaveChanges();
}

Only thing I'm not happy with is that it doesn't update child object. Fleet has FleetAttributes which are not updated like this. But I guess I can easily loop them...

EDIT this works for me:

// PUT api/fleet/5
public void Put(Fleet fleet)
{
    db.Entry(fleet).State = EntityState.Modified;
    foreach (var item in fleet.FleetAttributes)
    {
        db.Entry(item).State = EntityState.Modified;
    }
    db.SaveChanges();
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda