Reputation: 421
I have been struggling with getting a field value change to commit. Is there something obviously wrong here:
<HttpPost()>
Function Details(id As Guid?, model As RosterDetailModel) As ActionResult
If model.Action = RosterDetailModel.ActionOption.Save Then
If model.Action = RosterDetailModel.ActionOption.Save Then
Dim invalid = False ' initalize able to save
'check validations
Dim sFirstname = IIf(model.NameFirst Is Nothing, String.Empty, model.NameFirst).ToString().Trim()
If sFirstname = String.Empty Then
invalid = True
ModelState.AddModelError("NameFirst", "First Name is required.")
End If
If invalid = False Then
'save is ok to do
Using db As New BCData()
Dim userModel As New RosterDetailModel(db, id)
'Dim userModel As New RosterDetailModel
'userModel =
userModel.NameFirst = sFirstname
'db.ApplyCurrentValues(userModel)
'db.AcceptAllChanges()
db.SaveChanges()
'userModel.SaveChanges(db, id, userModel)
End Using
End If
End If
End If
Return View(model)
End Function
I see Entity Model Not Being Updated on SaveChanges has "The problem was I was referencing different instantiations of the Container (each manager created its own). Thus, the entity items were not attached to anything." .. im not sure what exactly i need to change. When i tried to do a Linq query and set values directly it would tell me the field is readonly.
If invalid = False Then
'save is ok to do
Using db As New BCData()
'Dim userModel As New RosterDetailModel(db, id)
Dim userModel = From studentusers In db.studentusers _
Where _
studentusers.studentGuid = id _
Select _
studentusers.cellPhone, _
studentusers.officePhone, _
studentusers.phone, _
studentusers.alternateEmail, _
studentusers.country, _
studentusers.zip, _
studentusers.state, _
studentusers.city, _
studentusers.address2, _
studentusers.address1, _
studentusers.ForumeMailNotificationPreferences, _
studentusers.magazineSubscribed, _
studentusers.avatar, _
studentusers.dateStudentActivated, _
studentusers.dateDownloadOn, _
studentusers.dateInstructorOn, _
studentusers.instructor, _
studentusers.ctcAdmin, _
studentusers.download, _
studentusers.accessLevel, _
studentusers.datecreated, _
studentusers.guidsignaturecookie, _
studentusers.password, _
studentusers.organization, _
studentusers.email, _
studentusers.lastname, _
studentusers.firstname, _
studentusers.groupGuid, _
studentusers.studentGuid
db.Attach(userModel)
'Dim userModel As New RosterDetailModel
'userModel =
userModel.FirstOrDefault.firstname = sFirstname '**<- **** READ ONLY ???**
'db.ApplyCurrentValues(userModel)
'db.AcceptAllChanges()
db.SaveChanges()
'userModel.SaveChanges(db, id, userModel)
End Using
Upvotes: 1
Views: 473
Reputation: 177133
Reload the userModel
from the database:
If invalid = False Then
Using db As New BlueCardData()
Dim userModel = (From studentuser In db.studentusers _
Where studentuser.studentGuid = id _
Select studentuser).Single()
'original userModel from DB is attached to context now
'change tracking will start from here
userModel.firstname = sFirstname
db.SaveChanges()
'EF detects change of firstname and will create UPDATE statement
End Using
Your second code doesn't work because you are projecting into an anonymous type and you cannot change properties of anonymous types. They are always readonly.
Upvotes: 1