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?
source share