How to update a model in a database from asp.net MVC2 using Entity Framework?

I am creating an ASP.NET MVC2 application and using the Entity Framework as an ORM. I am having problems updating an object in a database. Each time I try the .SaveChanges () entity, EF inserts a new row into the table, regardless of whether I want to update or insert. I tried to add (as in the following example) an object to the entity, but then I got

{"An object with a null EntityKey value cannot be attached to an object context."} 

Here is my simple function for inserts and updates (this is not entirely about vehicles, but it’s easier to explain it, although I don’t think that these effects generally respond) ...

  public static void InsertOrUpdateCar(this Vehicles entity, Cars car) { if (car.Id == 0 || car.Id == null) { entity.Cars.AddObject(car); } else { entity.Attach(car); } entitet.SaveChanges(); } 

I even tried to use AttachTo ("Cars", car), but I got the same exception.

Does anyone have any experience?

0
source share
3 answers

If you are updating an existing record, you must have an EntityKey in the instance of the object that you provide to your InsertOrUpdate method. Go back to your code and see if you can find the place where it was lost. I suspect that you present the user with a form for updating this object, and then map the response fields back to the Car object, but you do not pass the EntityKey with it (you probably don't want to display it to the user).

What you need to do is include the key in the form using the "hidden" input type. You can use the Html helper Html.Hidden ("Key field name", key field value) to make sure that it is submitted to the user form and then back to your Postal Code.

+1
source

I can give you an approximate reference, but your code above does not give me a ton of work.

Would you like to do something like:

 using(Entities dataModel = new Entities()) { if(car.Id == 0 || car.Id == null) { dataModel.AddToCars(car); /* There should be a generated method similar to this that just takes a car object minus the Primary Key */ } else { var selectedCar = dataModel.Cars.Where(x => x.Id == car.Id).FirstOrDefault(); if(selectedCar != null) { selectedCar.Name == car.Name; // Continue updating your car stuff } } dataModel.SaveChanges(); } 
+3
source

Another approach, which some may consider a little more elegant, is as follows:

 IContext context = ContextFactory.GetContext(); EntityRepo repo = new EntityRepo(context); //Entity Repository OtherTableEntity y = new OtherTableEntity() //this could be some other derived value you already have //that uniquely identifies the record (or foreign key or other value you want to update) Int32 id = 1234; //value to update to var z = repo.TableToUpdate.List().Where(x => x.FK_ID == y.FK_ID).FirstOrDefault(); if (z != null) { z.FK_ID = id; repo.Commit(); } 

Put a gap before and after repo.Commit(); and open the SQL query window and start the selection from the table before and after repo.Commit() .

Adding criteria is basically what you want. Call repo.EntityToUpdate.Add(entity) and repo.Commit() .

+1
source

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


All Articles