2011-11-15 29 views

Trả lời

13

Bạn có thể truy cập chúng thông qua ObjectStateEntry

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

originalValues ​​là DbDataRecord type. Làm thế nào để chuyển đổi nó thành loại thực thể? – JatSing

+0

@Sun nó không có loại thực thể. Bạn cần truyền các giá trị cho loại thích hợp. ví dụ 'var name = (string) originalValues ​​[" Name "];' – Eranga

27

@Eranga câu trả lời là lỗi thời cho EF 5. Đối với một số lý do, EF 5 không hoạt động tốt khi nhận được giá trị ban đầu bằng một tuyên bố như thế này:

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

giải pháp làm việc của tôi sử dụng phương pháp AsNoTracking() từ DbSet, như ví dụ dưới đây:

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

AsNoTracking() hiệu suất tốt hơn - google it – Moji

+0

Làm cách nào để nhận được giá trị được sửa đổi? –

+0

@AwaisMahmood Tôi nghĩ điều này xứng đáng với một câu hỏi riêng. –

6

Điều này có thể được tinh chế thêm những điều sau đây:

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

Các Where ở trên, tốt, phản ứng không cần thiết.

0

Tôi gặp phải vấn đề tương tự và AsNoTracking không phải là một lựa chọn cho tình huống của tôi vì vậy tôi đã đưa ra một cái gì đó hoạt động tốt cho tôi: đầu tiên "nhân bản" thực thể rồi thực hiện thay đổi.

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; 
} 

và sau đó so sánh bản sao với thay đổi.

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

Có một vài phiên bản Khuôn khổ thực thể được sử dụng.

Bản thân tôi thích Mã đầu tiên và với API rằng đó là dễ dàng như

_context.Entry(Entity).Reload(); 

Documents https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.103).aspx

của API cũ có một phương pháp Refresh trên ObjectContext mà có thể giúp đỡ trong trường hợp sử dụng nhất định

ObjectContext.Refresh(RefreshMode.StoreWins, Entity); 

Tài liệu https://msdn.microsoft.com/en-us/library/bb896255(v=vs.110).aspx

0

Câu trả lời này đề cập đến khung thực thể 6. Trong EF 6 có giá trị gốc và giá trị hiện tại https://msdn.microsoft.com/en-us/library/gg679512(v=vs.113).aspx Sau khi tìm kiếm và không tìm được câu trả lời hay, tôi đã đưa ra chức năng kiểm tra sau đây và nghĩ rằng tôi sẽ đăng nó cho những người khác cần làm tương tự.

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(); 
    } 
Các vấn đề liên quan