2013-07-11 36 views
1

我一直在閱讀大量試圖學習MVC4的文章,但我很難理解爲什麼我的實體沒有更新到數據庫。 我一直在嘗試修改MVC4 VS2012 Internet模板。MVC4 EF5實體屬性沒有更新SaveChanges()

所以,這裏的控制器動作:

[HttpPost, ActionName("Approve")] 
[Authorize] 
public ActionResult ApproveConfirmed(long id) 
{ 
    using (StudentiContext context = new StudentiContext()) 
    { 
     // context.Configuration.AutoDetectChangesEnabled = false; 
    var studente = (from d in context.STUDENTI_STRANIERI_MASTER_REG 
      where d.ID_PERSONA == id 
      select d).Single(); 

    STUDENTI_STRANIERI_MASTER_REG st2 = studente; 

    st2.ESITO = 1; 

    //studente.ESITO = 1; 
    var statos = context.Entry(studente).State; 
    Console.WriteLine("Before DetectChanges: {0}",statos); 
    //context.ChangeTracker.DetectChanges(); 

    context.Entry(studente).State = EntityState.Modified; 



    context.Entry(studente).CurrentValues.SetValues(st2); 

     // var tracked = context.ChangeTracker.Entries(); 

    context.Entry(studente).Property(o => o.ESITO).IsModified = true; 
    TryUpdateModel(studente); 
    context.SaveChanges(); 

    Console.WriteLine("After DetectChanges: {0}",statos); 

    return RedirectToAction("PrivateIndex"); 
    } 
} 

目的只是要更新一個屬性,ESITO並將其設置爲1。目前,它的值是2 這是在型號:

namespace MvcStudenti2.Models 
{ 
using System; 
using System.Collections.Generic; 

public partial class STUDENTI_STRANIERI_MASTER_REG 
{ 
    public long ID_PERSONA { get; set; } 
    public string COGNOME { get; set; } 
    public string NOME { get; set; } 
    public string SESSO { get; set; } 
    public System.DateTime DATA_NASCITA { get; set; } 
    public long ID_STATO_NASCITA { get; set; } 
    public string LUOGO_NASCITA_ESTERO { get; set; } 
    public string CODICE_FISCALE { get; set; } 
    public string TITOLO_POSSEDUTO { get; set; } 
    public Nullable<short> DURATA_TITOLO { get; set; } 
    public string VOTAZIONE { get; set; } 
    public string UNI_PROVENIENZA { get; set; } 
    public long ID_STATO_UNI { get; set; } 
    public string CERT_LINGUISTICA { get; set; } 
    public string CERT_PUNTEGGIO { get; set; } 
    public string NOTE { get; set; } 
    public System.DateTime DATA_RICHIESTA { get; set; } 
    public short ESITO { get; set; } 
    public string CDS_COD { get; set; } 
    public string EMAIL { get; set; } 
    public string NUMERO_TELEFONO { get; set; } 
    public string INDIRIZZO { get; set; } 
    public string CAP_INDIRIZZO { get; set; } 
    public string CITTA { get; set; } 
    public long ID_STATO_INDIRIZZO { get; set; } 
    public string DESCRIZIONE_CIT_NAZ { get; set; } 
    public Nullable<System.DateTime> DATA_COMPLETAMENTO_ATTESO { get; set; } 
    public Nullable<System.DateTime> ANNO_COMPLETAMENTO { get; set; } 
    public Nullable<short> DURATA_CORSO_COMPLETATO { get; set; } 
    public decimal GPA { get; set; } 
    public string ALTRI_TITOLI { get; set; } 
    public string MADRELINGUA { get; set; } 
    public Nullable<short> CERT_TOEFL_PUNT { get; set; } 
    public string CERT_FIRSTCERT_GRADE { get; set; } 
    public Nullable<short> CERT_FIRSTCERT_PUNT { get; set; } 
    public byte[] FILE_CV { get; set; } 
    public byte[] FILE_CARRIERA { get; set; } 
    public byte[] FILE_CERT_LINGUA { get; set; } 
    public byte[] FILE_DOC_IDENTITA { get; set; } 
    public string PWD { get; set; } 
    public string FILE_CV_NOME { get; set; } 
    public string FILE_CARRIERA_NOME { get; set; } 
    public string FILE_CERT_LINGUA_NOME { get; set; } 
    public string FILE_DOC_IDENTITA_NOME { get; set; } 
    public string FILE_CV_TIPO { get; set; } 
    public string FILE_CARRIERA_TIPO { get; set; } 
    public string FILE_CERT_LINGUA_TIPO { get; set; } 
    public string FILE_DOC_IDENTITA_TIPO { get; set; } 
    public Nullable<short> STATO { get; set; } 
    public Nullable<short> VALUTATO { get; set; } 
    public Nullable<short> ARCHIVIATO { get; set; } 
    public string CDS_COD_2 { get; set; } 
    public Nullable<short> MAIL_INVIATA { get; set; } 
    public string LINK_ULTIMO_CORSO { get; set; } 
    public Nullable<short> ATTIVO { get; set; } 
    public byte[] FILE_LETTERA_ACCETTAZIONE { get; set; } 
    public string FILE_LETTERA_ACCETTAZIONE_NOME { get; set; } 
    public string FILE_LETTERA_ACCETTAZIONE_TIPO { get; set; } 
} 
} 

無處不讀我發現SaveChanges()應該是足夠的,可能在EntityState.Modified之後。

我可以正確編輯實體,如果我將整個實體傳遞給Action,但在這種情況下,Approve視圖是建立在Detail模板上的,所以我沒有任何東西可以從它發佈(和我我寧願不要:我可以插入一個隱藏的字段併發布,但是我試圖從代碼更新單個字段,並且我不確定整個實體是否會被更新或覆蓋)。

statos轉到「修改」,如果我理解正確,因爲我已經對實體進行了查詢。

我不明白的另一件事是爲什麼ESITO在studente獲得更新 - 也就是在SaveChanges()之後恢復爲「2」。

是否檢測到屬性更改?正如其他地方所建議的那樣,我已將每個操作都包裝在一個using區塊中,以避免出現多個contextx /實例。

任何人都可以請指點我做錯了什麼?上面的代碼可能是過度冗餘的,但我一直在嘗試我發現的所有內容。

謝謝大家。

回答

0

以下是更改ESITO屬性所需的全部內容。

[HttpPost, ActionName("Approve")] 
[Authorize] 
public ActionResult ApproveConfirmed(long id) 
{ 
    using (StudentiContext context = new StudentiContext()) 
    { 
     // context.Configuration.AutoDetectChangesEnabled = false; 
    var studente = (from d in context.STUDENTI_STRANIERI_MASTER_REG 
      where d.ID_PERSONA == id 
      select d).Single(); 

    studente.ESITO = 1; 
    context.SaveChanges(); 

    return RedirectToAction("PrivateIndex"); 
    } 
}