1616using System . Linq ;
1717
1818#elif EFCORE
19+ using System ;
1920using Microsoft . EntityFrameworkCore ;
2021using Microsoft . EntityFrameworkCore . ChangeTracking ;
2122
@@ -56,8 +57,9 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
5657 {
5758 var name = record . GetName ( i ) ;
5859 var value = record . GetValue ( i ) ;
60+ var type = record . GetFieldType ( i ) ;
5961
60- if ( auditEntry . Parent . Configuration . UseNullForDBNullValue && value == DBNull . Value )
62+ if ( auditEntry . Parent . Configuration . UseNullForDBNullValue && value == DBNull . Value )
6163 {
6264 value = null ;
6365 }
@@ -74,14 +76,26 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
7476 auditEntry . Parent . Configuration . AuditEntryPropertyFactory ( new AuditEntryPropertyArgs ( auditEntry , objectStateEntry , string . Concat ( prefix , name ) , null , value ) ) :
7577 new AuditEntryProperty ( ) ;
7678
77- auditEntryProperty . Build ( auditEntry , string . Concat ( prefix , name ) , null , value ) ;
79+ if ( auditEntry . Parent . CurrentOrDefaultConfiguration . IgnoreEntityAddedDefaultValue && type != typeof ( object ) && value != null )
80+ {
81+ var checkDefaultValue = DefaultValue ( type ) ;
82+ if ( checkDefaultValue != null && checkDefaultValue . Equals ( value ) )
83+ {
84+ value = null ;
85+ }
86+ }
87+
88+ auditEntryProperty . Build ( auditEntry , string . Concat ( prefix , name ) , null , value ) ;
7889 auditEntry . Properties . Add ( auditEntryProperty ) ;
7990 }
8091 }
8192 }
93+
94+
95+
8296#elif EFCORE
83- /// <summary>Audit entity added.</summary>
84- /// <param name="objectStateEntry">The object state entry.</param>
97+ /// <summary>Audit entity added.</summary>
98+ /// <param name="objectStateEntry">The object state entry.</param>
8599 public static void AuditEntityAdded ( AuditEntry entry , EntityEntry objectStateEntry )
86100 {
87101 foreach ( var propertyEntry in objectStateEntry . Metadata . GetProperties ( ) )
@@ -92,13 +106,33 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt
92106 {
93107 var auditEntryProperty = entry . Parent . Configuration . AuditEntryPropertyFactory != null ?
94108 entry . Parent . Configuration . AuditEntryPropertyFactory ( new AuditEntryPropertyArgs ( entry , objectStateEntry , propertyEntry . Name , null , property . CurrentValue ) ) :
95- new AuditEntryProperty ( ) ;
109+ new AuditEntryProperty ( ) ;
110+
111+ var value = property . CurrentValue ;
96112
97- auditEntryProperty . Build ( entry , propertyEntry . Name , null , property . CurrentValue ) ;
113+ if ( entry . Parent . CurrentOrDefaultConfiguration . IgnoreEntityAddedDefaultValue && property . Metadata . FieldInfo != null && property . Metadata . FieldInfo . FieldType != typeof ( object ) && property . CurrentValue != null )
114+ {
115+ var checkDefaultValue = DefaultValue ( property . Metadata . FieldInfo . FieldType ) ;
116+ if ( checkDefaultValue != null && checkDefaultValue . Equals ( property . CurrentValue ) )
117+ {
118+ value = null ;
119+ }
120+ }
121+
122+ auditEntryProperty . Build ( entry , propertyEntry . Name , null , value ) ;
98123 entry . Properties . Add ( auditEntryProperty ) ;
99124 }
100125 }
101126 }
102127#endif
103- }
128+
129+ // https://stackoverflow.com/questions/2490244/default-value-of-a-type-at-runtime
130+ internal static object DefaultValue ( Type type )
131+ {
132+ if ( type . IsValueType )
133+ return Activator . CreateInstance ( type ) ;
134+
135+ return null ;
136+ }
137+ }
104138}
0 commit comments