Asp.net mvc 使用DBContext Entry.OriginalValues和Entry.NewValues记录更改的值

Asp.net mvc 使用DBContext Entry.OriginalValues和Entry.NewValues记录更改的值,asp.net-mvc,entity-framework,edit,dbcontext,Asp.net Mvc,Entity Framework,Edit,Dbcontext,我有一个文档库网站,希望在编辑文档对象时发送一封电子邮件,其中包含更改的摘要 数据库交互是使用DBContext的代码优先实体框架 以下是我到目前为止的情况: [HttpPost] public ActionResult Edit(Document document, bool sendEmail, string commentsTextBox) { if (ModelState.IsValid) { docsDB

我有一个文档库网站,希望在编辑文档对象时发送一封电子邮件,其中包含更改的摘要

数据库交互是使用DBContext的代码优先实体框架

以下是我到目前为止的情况:

    [HttpPost]
    public ActionResult Edit(Document document, bool sendEmail, string commentsTextBox)
    {

        if (ModelState.IsValid)
        {
            docsDB.Entry(document).State = EntityState.Modified;

            foreach (string propertyName in docsDB.Entry(document).OriginalValues.PropertyNames)
            {
                var OriginalValue = docsDB.Entry(document).OriginalValues.GetValue<object>(propertyName);
                var NewValue = docsDB.Entry(document).CurrentValues.GetValue<object>(propertyName);
                if (!OriginalValue.Equals(NewValue))
                {
                    //capture the changes
                }
            }

            docsDB.SaveChanges();
            if (sendEmail)
            {
               //sends email
            }
            return RedirectToAction("Index");

        }
[HttpPost]
公共操作结果编辑(文档、bool sendmail、字符串注释TextBox)
{
if(ModelState.IsValid)
{
docsDB.Entry(document).State=EntityState.Modified;
foreach(docsDB.Entry(document.OriginalValues.PropertyNames)中的字符串propertyName)
{
var OriginalValue=docsDB.Entry(document.OriginalValues.GetValue(propertyName);
var NewValue=docsDB.Entry(document).CurrentValues.GetValue(propertyName);
如果(!OriginalValue.Equals(NewValue))
{
//捕捉变化
}
}
docsDB.SaveChanges();
如果(发送电子邮件)
{
//发送电子邮件
}
返回操作(“索引”);
}
然而,OriginalValue和NewValue总是相同的——更新的值


除了像写入文件这样的骇客行为外,是否有其他方法可以在发布前捕获文档的状态?

要将更新的属性与数据库中的值进行比较,您必须从数据库中重新加载文档。您可以这样尝试:

[HttpPost]
public ActionResult Edit(Document document, bool sendEmail,
    string commentsTextBox)
{
    if (ModelState.IsValid)
    {
        var documentInDB = docsDB.Documents.Single(d => d.Id == document.Id);

        docsDB.Entry(documentInDB).CurrentValues.SetValues(document);

        foreach (string propertyName in docsDB.Entry(documentInDB)
                                        .OriginalValues.PropertyNames)
        {
            var OriginalValue = docsDB.Entry(documentInDB)
                                .OriginalValues.GetValue<object>(propertyName);
            var NewValue = docsDB.Entry(documentInDB)
                           .CurrentValues.GetValue<object>(propertyName);

            if (!OriginalValue.Equals(NewValue))
            {
                //capture the changes
            }
        }

        docsDB.SaveChanges();
        if (sendEmail)
        {
           //sends email
        }
        return RedirectToAction("Index");
    }
    // ...
}
[HttpPost]
公共行动结果编辑(文档、bool sendmail、,
字符串注释(文本框)
{
if(ModelState.IsValid)
{
var documentInDB=docsDB.Documents.Single(d=>d.Id==document.Id);
docsDB.Entry(documentInDB).CurrentValues.SetValues(document);
foreach(docsDB.Entry(documentInDB)中的字符串propertyName)
.OriginalValues.PropertyNames)
{
var OriginalValue=docsDB.Entry(documentInDB)
.OriginalValues.GetValue(propertyName);
var NewValue=docsDB.Entry(documentInDB)
.CurrentValues.GetValue(propertyName);
如果(!OriginalValue.Equals(NewValue))
{
//捕捉变化
}
}
docsDB.SaveChanges();
如果(发送电子邮件)
{
//发送电子邮件
}
返回操作(“索引”);
}
// ...
}