2017-01-11 9 views
1

(追加、変更、削除)は、C監査証跡を実装します。この問題は変更されたすべてのプロパティを監査するため、変更する必要がありますが、監査しないプロパティがいくつかあります。例:タイムスタンプなど。 これは私がやったことで、正常に動作します: 1)私はDBContext 2に別のSaveChanges()メソッドを作った)iはサーバで行われたすべてのアクションの監査ログを実装#

if (dbEntity.State == EntityState.Modified) 
      { 
       foreach (string propertyName in dbEntity.OriginalValues.PropertyNames) 
       { 
        if (!Equals(dbEntity.OriginalValues.GetValue<object>(propertyName), dbEntity.CurrentValues.GetValue<object>(propertyName))) 
        { 
         var log = new AuditLogDetailEntity() 
         { 
          Timestamp = timestamp, 
          Type = "M", // Modified 
          EntityName = tableName1, 
          PrimaryKeyValue = Convert.ToInt32(dbEntity.CurrentValues.GetValue<object>(primaryKeyName)), 
          PropertyName = propertyName,  
          OldValue = dbEntity.OriginalValues.GetValue<object>(propertyName) == null ? null : dbEntity.OriginalValues.GetValue<object>(propertyName).ToString(), 
          NewValue = dbEntity.CurrentValues.GetValue<object>(propertyName) == null ? null : dbEntity.CurrentValues.GetValue<object>(propertyName).ToString() 
         }; 
         changesCollection.Add(log); 
        } 
       } 
      }` 

この抽出物コード、すべてではないfuncionです。 は、私はそれは私が監査する `tをフィールドを求めて、内部の検証を行うこともできますが、それをやってのより完全な方法はありますか?たぶんクラスにデータアノテーションを追加したり、何か他のものを追加することがあります。 ありがとうございます。

答えて

0

あなたはそのために[System.ComponentModel.DataAnnotations.Schema.NotMapped]属性を使用することができます。

+0

を持っているかどうかを確認できますか?どのDataAnnotationを使用しますか?おかげで、このようにそれを使用し –

+0

: クラスAuditLogDetailEntity { [System.ComponentModel.DataAnnotations.Schema.NotMapped] のDateTimeタイムスタンプ{取得します。セット; } // etc } – Simmetric

0

カスタム属性

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 
public class UnMappedAttribute : Attribute 
{ 
} 

を作成し、各プロパティがどのように

foreach (string propertyName in dbEntity.OriginalValues.PropertyNames) 
{ 
    if(!dbEntity.Entity.GetType().GetCustomAttributes(typeof(UnMappedAttribute), true).Any()) 
    { 
     continue; 
    } 

    if (!Equals(dbEntity.OriginalValues.GetValue<object>(propertyName), dbEntity.CurrentValues.GetValue<object>(propertyName))) 
    { 
     //..... 
    } 
} 
関連する問題