实体linq savechanges问题

时间:2012-02-09 18:19:10

标签: linq entity-framework 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有“问题是我引用了Container的不同实例化(每个管理器创建了自己的实例)。因此,实体项目没有附加到任何东西。” ..我不知道到底需要改变什么。当我尝试执行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 个答案:

答案 0 :(得分: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

您的第二个代码不起作用,因为您正在投射到匿名类型,并且您无法更改匿名类型的属性。他们总是只读。