2012-02-09 14 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

+0

@phoenixAZ:匿名タイプへの射影はあなたの 'Select studentusers.cellPhone、...'です。これで匿名オブジェクトを作成しています。 'StudentUser'エンティティと同じプロパティを持ちますが、それはもう一つのタイプです。 – Slauma