How to update an object in a controller using EF?

I searched on google, but this seems too obvious, and no one talks about it.

I have a repository for my table and I want to be able to update my db.

In Linq2Sql, you have something like:

public void SaveCar(Car car) { if (carTable.GetOriginalEntityState(car) == null) { carTable.Attach(product); carTable.Context.Refresh(RefreshMode.KeepCurrentValues, car); } carTable.ContextSubmitChanges(); } 

and in the controller, just call this method in the POST Edit method.

How do I do something like this in EF? The best way.

I see the code using TryUpdateModel (model), but I'm not sure if this is an update that I have in the database, or I must first select the object and update it using FormCollection ...

I am very confused, I just need to get the car from the form and update the car with the same ID from db. So what should I do in the controller and storage?

Thanks.

EDIT: If I were unclear, I really don't know what I set, what I need to convert to EF. I just want to know how I update an instance of an object using EF (EFCodeFirst is what I use). How to get instance from form and update it in db.

+4
source share
2 answers

A month, no response, answering machine time.

The answer to my question is:

There is something like in the controller:

 if (ModelState.IsValid) repo.SaveCar(car); 

and in the repo:

 context.Entry(Car).State = EntityState.Modified; 

Only this, that way to save objects.

+2
source

The TryUpdateModel method can be used to assign values ​​to an object using the mapping between its property names and the values ​​specified in the web request. With this, you can achieve the same behavior by doing something like this:

 [HttpPost] public ActionResult Edit(int id, FormCollection form) { Car entity = new Car { Id = id }; // This will attach the car entity to the content in the unchanged state. this.EntityFrameworkObjectContext.Cars.Attach(car); TryUpdateModel(entity, form.ValueProvider.ToXYZ()); // Can't remember the exact method signature right now this.EntityFrameworkObjectContext.SaveChanges(); ... } 

When you attach an object to a context, you basically just notify the context in which you have an entity that it should take care of. Once the object is attached, the context will track changes made to it.

But this method is only suitable for scalar properties, since navigation properties are not updated using this method. If you have foreign key properties (for example, a Product assigned to a category also has a CategoryId property that binds them), you can still use this method (since the navigation property is displayed using the scalar property).

Edit: Another way is to accept the Car instance as a parameter:

 [HttpPost] public ActionResult Edit(int id, Car car) { Car entity = new Car { Id = id }; // This will attach the car entity to the content in the unchanged state. this.EntityFrameworkObjectContext.Cars.Attach(entity); // It is important that the Car instance provided in the car parameter has the // the correct ID set. Since the ApplyCurrentValues will look for any Car in the // context that has the specified primary key of the entity supplied to the ApplyCurrentValues // method and apply it values to those entity instance with the same ID (this // includes the previously attached entity). this.EntityFrameworkObjectContext.Cars.ApplyCurrentValues(car); this.EntityFrameworkObjectContext.SaveChanges(); ... } 

You can also collapse your own ModelBinder, which actually refers to your Entity Framework context and checks if Car.Id has been specified in the form / request. If an identifier is present, you can capture the object to update directly from the context. This method requires some effort, since you must first make sure that you are looking for an identifier, and then apply all the specified property values. If you are interested, I can give you some examples for this.

+2
source

Source: https://habr.com/ru/post/1341562/


All Articles