2012-02-09 131 views
1

我一直在努力获取字段值更改提交。有什么明显错误的位置:实体linq savechanges问题

<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 

我看到Entity Model Not Being Updated on SaveChanges有“但问题是,我引用了容器的不同实例(每个经理创建了自己的)。因此,实体项目未连接到任何东西。” ..我不知道我到底需要改变什么。当我试图做一个Linq查询并直接设置值时,它会告诉我该字段是只读的。

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 

回答

1

重新加载userModel从数据库中:

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 

你的第二个代码不能正常工作,因为你是伸入匿名类型,你不能改变匿名类型的属性。它们只是只读的。

+0

感谢它在你的解决方案中工作 - 我错过了我投影到匿名类型的细节。看起来代码是相似的。 – phoenixAZ 2012-02-12 18:02:15

+0

@phoenixAZ:投影到匿名类型是你的'选择studentusers.cellPhone,...'。你正在创建一个匿名对象。它与你的'StudentUser'实体具有相同的属性,但它仍然是另一种类型。 – Slauma 2012-02-12 19:56:49