JohnnyBizzle
JohnnyBizzle

Reputation: 979

LINQ update method doesn't update the Db

   Public Sub UpdateStaff(ByVal sr As StaffRecord)
             Dim oldSr As StaffRecord
             Dim q = From staff In db.StaffRecords Where staff.Employee_Number = sr.Employee_Number Select staff

    oldSr = q.First

    oldSr.Address_Line1 = sr.Address_Line1
    oldSr.Address_Line2 = sr.Address_Line2
    oldSr.Address_Line3 = sr.Address_Line3
    oldSr.Town_Or_City = sr.Town_Or_City
    oldSr.Contact1Name = sr.Contact1Name

    db.SubmitChanges()

End Sub

The helper function I have written seems to do everything I want apart from update the db. Stepping through the code, the oldSr is updated by the new sr parameter but no update on submit changes.

Upvotes: 1

Views: 835

Answers (2)

Kevin Nelson
Kevin Nelson

Reputation: 7663

If Matthew's fix doesn't work...I know what you have there is not Entity Framework 1, but in EF1 there was a method, ApplyPropertyChanges, that you could call to let it know that an Entity had been updated:

db.ApplyPropertyChanges("EntityTypeName",oldSr);
db.SaveChanges();

I'm not sure about what you are using, but I'm guessing that there is a similar method that you could use to force the context to recognize the change.

[EDIT: add comment] - I hope it's not bad form to post something that isn't truly an answer but just a "look for this kind of method" answer. If so, I apologize.

Kevin

Upvotes: 0

Matthew Vines
Matthew Vines

Reputation: 27561

Give this a shot, I think your context is being lost when you set q to oldSr.

Public Sub UpdateStaff(ByVal sr As StaffRecord)
             Dim q = (From staff In db.StaffRecords Where staff.Employee_Number = sr.Employee_Number Select staff).Single();

q.Address_Line1 = sr.Address_Line1
q.Address_Line2 = sr.Address_Line2
q.Address_Line3 = sr.Address_Line3
q.Town_Or_City = sr.Town_Or_City
q.Contact1Name = sr.Contact1Name

db.SubmitChanges()

End Sub

Upvotes: 1

Related Questions