Asked by:
Entity wont save changes no mattter what but adds records fine

Question
-
User-1868255333 posted
public Patient getPatientByPatientNewId(int newId) { Patient patient = new Patient(); if (newId == -1) { patient = new Patient(); } else { using (var myContext = new SMBASchedulerEntities(this.Connectionstring)) { patient = myContext.Patients .Where(w => w.ID == newId).FirstOrDefault(); } } return patient; }
Entity framework will not update an entity no matter what I do it will add a record fine but not update existing one
private void btnSave_Click(object sender, EventArgs e) { Patient _newpatient = new Patient(); if(txtID.Text !="") _newPatient = SourceDal.getPatientByPatientNewId(Convert.ToInt32(txtID.Text)); _newPatient.emailAddress = "test UPDATE"; _newPatient.Gender = "M"; SourceDal.SaveChanges(); }
Here is my funciton that retrieves the single record so that i can make a change to iit , when i look futher at my save changes method it says that the patient table remains unchanged even though it has changed via the code above
Thursday, January 4, 2018 2:44 PM
All replies
-
User753101303 posted
Hi,
And you are sure the value for txtID.Text is correct ? If not, you are just creating and change a new object and EF doesn't know anything about it. If yes it could also depend on what does getPatientByPatientNewId especially if txtID.Text doesn't match an existing id (returning a new Patient object would give the same wrong behavior).
I very often suggest to not try to debug your code by just reading it. Instead if the app doesn't show any error but doesn't behave as expected you should always start by using https://docs.microsoft.com/fr-fr/visualstudio/debugger/debugger-feature-tour to see what your code does and if it matches what you expected.
Edit: IMHO you should definitively restart fresh. In particular using a single method to return a new object, an existing object or a new object because the key doesn't match is a terrible idea.
Thursday, January 4, 2018 3:17 PM -
User475983607 posted
It looks like you have two different contexts? There is one context that returns the patient object and is disposed by the using block in a data layer? Then there is another static context in the SourceDal object which is a member of the page class?
Try passing the update values the data access layer rather than having the context in two different layers.
Thursday, January 4, 2018 6:08 PM -
User1120430333 posted
You are using a Web application and Web applications are stateless. Therefore, EF is in a disconnected state.
Update Existing Entity using DBContext in Disconnected Scenario:
http://www.entityframeworktutorial.net/EntityFramework4.3/update-entity-using-dbcontext.aspx
Thursday, January 4, 2018 11:28 PM -
User-832373396 posted
Hi <g class="gr_ gr_9 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="9" data-gr-id="9">david</g>,
Sir, it doesn't do update operation from your code;
and it only changes a model value but not attach this model to dbcontext;
so, please add this code:
private void btnSave_Click(object sender, EventArgs e) { Patient _newpatient = new Patient(); if(txtID.Text !="") _newPatient = SourceDal.getPatientByPatientNewId(Convert.ToInt32(txtID.Text)); _newPatient.emailAddress = "test UPDATE"; _newPatient.Gender = "M"; var myContext = new SMBASchedulerEntities(this.Connectionstring) myContext .Entry(_newPatient).State = EntityState.Modified;//attach to dbcontext with updated model db.SaveChanges(); SourceDal.SaveChanges(); }
Then it will work.
With regards, Angelina Jolie
Friday, January 5, 2018 12:12 PM