I am not sure exactly how to word the Title so I would also appreciate any thoughts on that part and will update this accordingly.
Using C#, standard console program.
I have a database object with a number of fields, any or all of them may be being changed. I have another object with the same fields that contains the new data. I can go through one by one assigning them and it work, but if i just do the entire thing at once it does not work.
Probably clearer with some code. the code that DOES work first: I have all of the data that I need updated in newStop.
I go out and get the data from the database and store it in findStop2
.
I then can go through all the fields one by one (only one is shown here) and assign the values from newStop
to findStop2
.
When I do a db.SaveChanges
everything works.
var findStop2 = (from s in db.stop_details
where s.customer_id == customer_id && s.stop_id == newStop.stop_id
select s)
.First();
//if it did not error out it found it
findStop2.ship_date = newStop.ship_date;
But there are a lot of fields. Why can I not just use:
findStop2 = newStop;
When I do this and do a SaveChanges
there is no error thrown but the database also is not updated.
Solution -- Final changed code that works:
var findStop2 = (from s in db.stop_details where
s.customer_id == customer_id && s.stop_id == newStop.stop_id
select s).First();
//if it did not error out it found it
newStop.id = findStop2.id;
db.Entry(findStop2).CurrentValues.SetValues(newStop);