2012-03-09 53 views
0

MVC 3,VB.NET,RAZOR應用程序,使用EF。我在我的post函數中有一個問題,數據庫根本沒有更新......在db.savechanges()中使用break我看看這個變量,所有正確的信息都包含在UpdateModel()部分中。但沒有骰子的代碼執行,並沒有返回任何錯誤,所以一切看起來不錯,但當我看着數據庫表,它根本沒有改變,所有的舊值仍然存在..功能如下:使用EF模型數據庫不更新使用updateModel

<AcceptVerbs(HttpVerbs.Post)> 
    Function EditCourse(ByVal _eCourse As cours) As ActionResult 
     Dim id As Integer = _eCourse.course_id 

      Dim _filename As String = String.Empty 
      Dim _guid As String = String.Empty 

      Dim _count As Integer = 0 
      Dim _courseFiles As cours = db.courses.Where(Function(f) f.course_ref = _eCourse.course_ref).First 

      Dim _file1 As String = _courseFiles.handoutFile1 
      Dim _file2 As String = _courseFiles.handoutFile2 
      Dim _file3 As String = _courseFiles.handoutFile3 
      Dim _file4 As String = _courseFiles.handoutFile4 
      Dim _file5 As String = _courseFiles.handoutFile5 
      Dim _file6 As String = _courseFiles.handoutFile6 
      Dim _file7 As String = _courseFiles.handoutFile7 
      Dim _file8 As String = _courseFiles.handoutFile8 
      For Each File As String In Request.Files 
       _count += 1 
       Dim hpf As HttpPostedFileBase = TryCast(Request.Files(File), HttpPostedFileBase) 
       If hpf.ContentLength = 0 Then 
        Continue For 
       End If 

       Dim savedfileName As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + hpf.FileName 
       hpf.SaveAs(savedfileName) 
       _filename = hpf.FileName 

       Select Case _count 
        Case Is = 1 
         If Not String.IsNullOrWhiteSpace(_file1) Then 
          If Not String.Compare(_eCourse.handoutFile1, _file1) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile1) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file1 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile1 = _filename 

        Case Is = 2 
         If Not String.IsNullOrWhiteSpace(_file2) Then 
          If Not String.Compare(_eCourse.handoutFile2, _file2) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile2) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file2 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile2 = _filename 

        Case Is = 3 
         If Not String.IsNullOrWhiteSpace(_file3) Then 
          If Not String.Compare(_eCourse.handoutFile3, _file3) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile3) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file3 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile3 = _filename 

        Case Is = 4 
         If Not String.IsNullOrWhiteSpace(_file4) Then 
          If Not String.Compare(_eCourse.handoutFile4, _file4) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile4) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file4 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile4 = _filename 

        Case Is = 5 
         If Not String.IsNullOrWhiteSpace(_file5) Then 
          If Not String.Compare(_eCourse.handoutFile5, _file5) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile5) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file5 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile5 = _filename 

        Case Is = 6 
         If Not String.IsNullOrWhiteSpace(_file6) Then 
          If Not String.Compare(_eCourse.handoutFile6, _file6) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile6) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file6 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile6 = _filename 

        Case Is = 7 
         If Not String.IsNullOrWhiteSpace(_file7) Then 
          If Not String.Compare(_eCourse.handoutFile7, _file7) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile7) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file7 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile7 = _filename 

        Case Is = 8 
         If Not String.IsNullOrWhiteSpace(_file8) Then 
          If Not String.Compare(_eCourse.handoutFile8, _file8) = 0 AndAlso Not String.IsNullOrWhiteSpace(_eCourse.handoutFile8) Then 
           Dim FileToDelete As String = Path.Combine(AppDomain.CurrentDomain.BaseDirectory) + "\CourseHandouts\" + _file8 
           If System.IO.File.Exists(FileToDelete) = True Then 
            System.IO.File.Delete(FileToDelete) 
           End If 
          End If 
         End If 
         _eCourse.handoutFile8 = _filename 

       End Select 

      Next 

      UpdateModel(_eCourse) 
      db.SaveChanges() 


      Return RedirectToAction("CourseIndex") 

     End Function 

任何想法,爲什麼這會出錯?????

回答

1

您沒有將您的_eCourse附加到您的上下文,因此它不會更新它。 UpdateModel我不相信這裏所要求的,因爲它只是簡單地將你的發佈表單值賦值給你已經擁有的模型,因爲eCourse是一個參數。這樣做(在這裏手機對不起) DB.Entry(_eCourse).STATE = EntityState.Modified

然後保存更改

+0

VS心不是順心的.STATE部分,是說其不是成員xxxxxxxx.cours在所有...我有這條線現在閱讀db.courses(_eCourse.course_id).State = EntityState.Modified和沒有骰子...應該指出,我試圖db.entry(_ecourse)等,它不採用Entry參數。進一步我不得不指定course_id,因爲它想要一個整數那裏.. – Skindeep2366 2012-03-09 05:52:38

+0

我假設你使用ObjectContext而不是DbContext。你使用什麼版本的EF?如果它是一個新的對象,你必須'添加'它,一個現有的對象,你需要'附加修改'。我強烈建議安裝DbContext模板,因爲api比DbContext而不是ObjectContext更容易處理,但至少需要EF 4.0。這是一個使用ObjectContext更新的例子(請參閱更新方法)http://stackoverflow.com/questions/3594515/how-to-update-an-entity-in-entity-framework-4-net/3594608#3594608如果你需要使用dbcontext讓我知道一些方向。 – 2012-03-09 14:52:29

相關問題