如何检查哪些属性/项被修改的,哪些不是在EF 6编辑记录 [英] How to check that which properties/entries are modified and which are not on Editing a record in EF 6

查看:120
本文介绍了如何检查哪些属性/项被修改的,哪些不是在EF 6编辑记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用EF 6.一个MVC 5条申请编辑自动生成的POST方法是:

I am developing an MVC 5 application using EF 6. The auto-generated POST method for Edit is:

public ActionResult Edit([Bind(Include = "Id,Name,Address,Phone,SSNo,D1")] ABC abc)
{
     if (ModelState.IsValid)
     {
         db.Entry(abc).State = EntityState.Modified;
         db.SaveChanges();
     }
     return View(abc);
} 

有任何程序/方法,通过它我可以得到哪些项被修改,哪些是这些条目的原始值。我试图标记为<一个回答方法href=\"http://stackoverflow.com/questions/3265257/getting-all-changes-made-to-an-object-in-the-entity-framework\">this的问题,但虽然已经作了修改它没有得到任何改变,即循环不会迭代。

Is there any procedure/method by which I can get that which entries are modified and what were the original values of those entries. I have tried the method marked as an answer in this question but it didn't get any changes although changes have been made, i.e. the loop does not iterates.

我的code是:

 public ActionResult Edit([Bind(Include = "Id,Name,Address,Phone,SSNo,D1")] ABC abc)
 {
     if (ModelState.IsValid)
     {
          db.ABCs.Attach(abc);
          var myObjectState = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntry(abc);
          var modifiedProperties = myObjectState.GetModifiedProperties(); 
          foreach (var propName in modifiedProperties)
          {
                Console.WriteLine("Property {0} changed from {1} to {2}",
                propName,
                myObjectState.OriginalValues[propName],
                myObjectState.CurrentValues[propName]);
          }
          Console.ReadKey();
          db.Entry(abc).State = EntityState.Modified;
          db.SaveChanges();
          return Json(abc);
     }
     return View(abc);
 }

ABC模型包含了比方法的参数中提到的其他许多价值观,但我只有通过那些可编辑

ABC model contains many values other than mentioned in method parameters, but I am only passing those which can be edited

我所要做的是在我的对文档所做的所有更改数据库维护日志。我想只包括在日志中修改前修改和原始值不完整的记录。

What I am trying to do is to maintain a log in my database of all the changes made to a document. I want to include only the changes and the original values before modification in the log not the complete record.

我怎样才能得到呢?

推荐答案

我实现了这个扩展方法适合您:

I implemented this extension method for you:

namespace YourProject.Extensions
{
    public static class ModelExtensions
    {
        public static IEnumerable<KeyValuePair<string, object>> ModifiedValues<T>(this T obj, T modifiedObject) 
        {
            foreach (var property in typeof(T).GetProperties().Where(p => !p.GetGetMethod().IsVirtual))
            {
                if (property.GetValue(obj).ToString() != property.GetValue(modifiedObject).ToString())
                {
                    yield return new KeyValuePair<string, object>(property.Name, property.GetValue(modifiedObject));
                }
            }
        }
    }
}

这是既不是最快,也不是最有效的,但在我的测试工作。

It's neither the fastest nor the most efficient, but worked in my tests.

使用:

var originalEntity = await db.Entities.AsNoTracking().FirstOrDefaultAsync(e => e.EntityId == modifiedEntity.EntityId);
var modifiedValues = originalEntity.ModifiedValues<MyEntity>(modifiedEntity).ToList();

这篇关于如何检查哪些属性/项被修改的,哪些不是在EF 6编辑记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆