Skip to content

Commit fb68afc

Browse files
Added IsKey property
1 parent a527b24 commit fb68afc

File tree

5 files changed

+20
-4
lines changed

5 files changed

+20
-4
lines changed

src/shared/Z.EF.Plus.Audit.Shared/Audit/AuditEntityAdded.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
7777
var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ?
7878
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
7979
new AuditEntryProperty();
80+
auditEntryProperty.IsKey = objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name);
8081

81-
if (auditEntry.Parent.CurrentOrDefaultConfiguration.IgnoreEntityAddedDefaultValue && type != typeof(object) && value != null)
82+
if (auditEntry.Parent.CurrentOrDefaultConfiguration.IgnoreEntityAddedDefaultValue && type != typeof(object) && value != null)
8283
{
8384
var checkDefaultValue = DefaultValue(type);
8485
if (checkDefaultValue != null && checkDefaultValue.Equals(value))
@@ -109,9 +110,9 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt
109110
{
110111
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
111112
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) :
112-
new AuditEntryProperty();
113-
114-
var value = property.CurrentValue;
113+
new AuditEntryProperty();
114+
auditEntryProperty.IsKey = property.Metadata.IsKey();
115+
var value = property.CurrentValue;
115116

116117
if (entry.Parent.CurrentOrDefaultConfiguration.IgnoreEntityAddedDefaultValue && property.Metadata.FieldInfo != null && property.Metadata.FieldInfo.FieldType != typeof(object) && property.CurrentValue != null)
117118
{

src/shared/Z.EF.Plus.Audit.Shared/Audit/AuditEntityDeleted.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectS
7777
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
7878
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) :
7979
new AuditEntryProperty();
80+
auditEntryProperty.IsKey = objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name);
8081

8182
auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null);
8283
entry.Properties.Add(auditEntryProperty);
@@ -99,6 +100,7 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE
99100
new AuditEntryProperty();
100101

101102
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null);
103+
auditEntryProperty.IsKey = property.Metadata.IsKey();
102104
entry.Properties.Add(auditEntryProperty);
103105
}
104106
}

src/shared/Z.EF.Plus.Audit.Shared/Audit/AuditEntityModified.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
105105
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
106106
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) :
107107
new AuditEntryProperty();
108+
auditEntryProperty.IsKey = isKey;
108109

109110
auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentRecord, i);
110111
entry.Properties.Add(auditEntryProperty);
@@ -145,6 +146,7 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr
145146
new AuditEntryProperty();
146147

147148
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property);
149+
auditEntryProperty.IsKey = property.Metadata.IsKey();
148150
entry.Properties.Add(auditEntryProperty);
149151

150152
if (property.IsModified)

src/shared/Z.EF.Plus.Audit.Shared/AuditEntryProperty.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ public string NewValueFormatted
227227
public object OldValue;
228228
#endif
229229

230+
/// <summary>Gets or sets a value indicating whether this property is part of the key (NotMapped).</summary>
231+
/// <value>True if this property is part of the key, false if not (NotMapped).</value>
232+
[NotMapped]
233+
public bool IsKey { get; set; }
234+
230235
/// <summary>Gets or sets the old value audited formatted.</summary>
231236
/// <value>The old value audited formatted.</value>
232237
[Column("OldValue", Order = 4)]

src/shared/Z.EF.Plus._Core.Shared/EFCore/CreateEntity/CreateEntityDataReader.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ public override T GetFieldValue<T>(int ordinal)
204204
{
205205
value = new Guid(valueByteArray);
206206
}
207+
else if (typeof(T) == typeof(TimeSpan) && value is string valueString)
208+
{
209+
TimeSpan timeSpan;
210+
TimeSpan.TryParse(valueString, out timeSpan);
211+
value = timeSpan;
212+
}
207213

208214
return (T)value;
209215
}

0 commit comments

Comments
 (0)