2011-11-15 45 views

回答

13

您可以訪問他們通過ObjectStateEntry

var originalValues = context 
     .ObjectStateManager.GetObjectStateEntry(myEntity).OriginalValues; 
+0

originalValues是DbDataRecord類型。如何將其轉換爲實體類型? – JatSing

+0

@孫它沒有實體類型。您需要將這些值轉換爲適當的類型。例如'var name =(string)originalValues [「Name」];' – Eranga

27

@Eranga答案是過時的EF 5.由於某些原因,EF 5並沒有得到使用的語句像這樣原來的值時,工作得很好:

var originalValues = context.Entry(myEntity).OriginalValues; 

我的工作溶液使用AsNoTracking()方法從DbSet,如下面的例子:

var originalEntity = context.MyEntities.AsNoTracking().FirstOrDefault(me => me.MyEntityID == myEntity.MyEntityID); 
+1

AsNoTracking()更好的表現 - 谷歌它 – Moji

+0

如何獲得修改後的值? –

+0

@AwaisMahmood我認爲這值得一個單獨的問題。 –

6

這可以進一步改進,以執行以下操作:不需要

var originalEntity = context.MyEntities.AsNoTracking() 
     .FirstOrDefault(me => me.MyEntityID == myEntity.MyEntityID); 

在以上,良好,響應該Where

0

我遇到了類似的問題,AsNoTracking不適合我的情況,所以我提出了一些對我來說足夠好的方法:首先「克隆」實體,然後進行更改。

public T Clone<T>(T entity) 
    where T : class, new() { 

    var clone = new T(); 

    var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy) 
    .Where(a => a.CanRead && 
       a.CanWrite && 
       a.GetMethod.IsFinal); 

    foreach (var property in properties) {  
    property.SetValue(clone, property.GetValue(entity)); 
    } 

    return clone; 
} 

然後再比較克隆與改變。

public string GenerateChangeText<T>(T original, T current) 
    where T : class, new() { 

    var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy) 
    .Where(a => a.CanRead && 
       a.CanWrite && 
       a.GetMethod.IsFinal); 

    var changes = string.Empty; 

    foreach (var property in properties) { 

    var originalValue = property.GetValue(original); 
    var currentValue = property.GetValue(current); 

    if (originalValue == null && currentValue == null) continue; 
    if ((originalValue != null && !originalValue.Equals(currentValue)) || 
     (currentValue != null && !currentValue.Equals(originalValue))) { 

     changes += $" changed {property} from {original ?? "NULL"} to {current ?? "NULL"}."; 
    } 
    } 

    return changes; 
} 
0

本答案涉及實體框架6.在EF 6中有一個原始值和當前值https://msdn.microsoft.com/en-us/library/gg679512(v=vs.113).aspx在查找並未找到一個好的答案後,我想出了以下測試函數,並認爲我會將它發佈給其他需要執行相同。

private void test() 
    { 
     // table has a field Description of type varchar(200) 
     WDMDBEntities context = new WDMDBEntities(); 
     var query = context.Brands; 
     List<Brand> records = query.ToList(); 
     if (records.Count > 0) 
     { 
      Brand currentRecord = records[0]; 
      currentRecord.Description = "some new text"; 
      string originalValue = null; 
      switch (context.Entry(currentRecord).State) 
      { 
       case System.Data.Entity.EntityState.Added: 
        originalValue = null; 
        break; 
       case System.Data.Entity.EntityState.Deleted: 
       case System.Data.Entity.EntityState.Detached: 
       case System.Data.Entity.EntityState.Modified: 
       case System.Data.Entity.EntityState.Unchanged: 
        originalValue = context.Entry(currentRecord).Property(u => u.Description).OriginalValue; 
        break; 
      } 
     } 
     context.Dispose(); 
    } 
相關問題