在SaveChanges()上不更新MVC4 EF5实体属性

时间:2013-07-11 15:25:13

标签: asp.net asp.net-mvc asp.net-mvc-4 entity-framework-5

我一直在阅读很多试图学习MVC4的文章,但我很难过为什么我的实体没有更新到数据库。 我一直在尝试修改MVC4 VS2012 Internet模板。

所以,这是控制器动作:

[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中获得更新-also-,但在SaveChanges()之后会恢复为“2”。

是否检测到属性更改?我按照其他地方的建议将每个Action包装在using块中,这样就不会有多个contextx / instance。

有谁能请我指出我做错了什么?上面的代码可能过度冗余,但我一直在尝试在SO上找到的所有内容。

谢谢大家。

1 个答案:

答案 0 :(得分: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");
    }
}