Skip to content

Commit 661cb72

Browse files
author
zzzprojects
committed
Fix issue with Audit and some property value generated later
Fix issue with Audit and some property value generated later
1 parent c2afea3 commit 661cb72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1230
-627
lines changed

src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityAdded.cs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,6 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
4141
entry.Build(audit, objectStateEntry);
4242
entry.State = AuditEntryState.EntityAdded;
4343

44-
// CHECK if the key should be resolved in POST Action
45-
#if EF5 || EF6
46-
if (objectStateEntry.EntityKey.IsTemporary)
47-
{
48-
entry.DelayedKey = objectStateEntry;
49-
}
50-
AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues);
51-
#elif EFCORE
52-
// TODO: We must check if the key IsTemporary! We can maybe use flag...
53-
//if (!objectStateEntry.IsKeySet)
54-
//{
55-
entry.DelayedKey = objectStateEntry;
56-
//}
57-
AuditEntityAdded(entry, objectStateEntry);
58-
#endif
59-
6044
audit.Entries.Add(entry);
6145
}
6246

src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityDeleted.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System;
99
using System.Data.Common;
1010
using System.Linq;
11-
1211
#if EF5
1312
using System.Data.Objects;
1413

@@ -35,19 +34,19 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
3534
#endif
3635
{
3736
var entry = audit.Configuration.AuditEntryFactory != null ?
38-
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) :
39-
new AuditEntry();
37+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) :
38+
new AuditEntry();
4039

4140
entry.Build(audit, objectStateEntry);
4241
entry.State = AuditEntryState.EntityDeleted;
4342

43+
audit.Entries.Add(entry);
4444

4545
#if EF5 || EF6
4646
AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues);
4747
#elif EFCORE
4848
AuditEntityDeleted(entry, objectStateEntry);
4949
#endif
50-
audit.Entries.Add(entry);
5150
}
5251

5352
#if EF5 || EF6
@@ -76,8 +75,8 @@ public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectS
7675
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
7776
{
7877
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
79-
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) :
80-
new AuditEntryProperty();
78+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) :
79+
new AuditEntryProperty();
8180

8281
auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null);
8382
entry.Properties.Add(auditEntryProperty);

src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityModified.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
9292
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) :
9393
new AuditEntryProperty();
9494

95-
auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue);
95+
auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentRecord, i);
9696
entry.Properties.Add(auditEntryProperty);
9797
}
9898
}
@@ -115,7 +115,7 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr
115115
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) :
116116
new AuditEntryProperty();
117117

118-
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue);
118+
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, propertyEntry);
119119
entry.Properties.Add(auditEntryProperty);
120120
}
121121
}

src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipAdded.cs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,63 +36,62 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
3636
#endif
3737
{
3838
var entry = audit.Configuration.AuditEntryFactory != null ?
39-
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) :
40-
new AuditEntry();
39+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) :
40+
new AuditEntry();
4141

4242
entry.Build(audit, objectStateEntry);
4343
entry.State = AuditEntryState.RelationshipAdded;
4444

45-
var values = objectStateEntry.CurrentValues;
45+
audit.Entries.Add(entry);
46+
}
4647

48+
#if EF5 || EF6
49+
public static void AuditRelationAdded(Audit audit, AuditEntry entry, ObjectStateEntry objectStateEntry)
50+
#elif EFCORE
51+
public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
52+
#endif
53+
{
54+
var values = objectStateEntry.CurrentValues;
4755

4856
var leftKeys = (EntityKey) values.GetValue(0);
4957
var rightKeys = (EntityKey) values.GetValue(1);
5058

51-
if (leftKeys.IsTemporary || rightKeys.IsTemporary)
52-
{
53-
entry.DelayedKey = objectStateEntry;
54-
}
55-
else
59+
var leftRelationName = values.GetName(0);
60+
var rightRelationName = values.GetName(1);
61+
62+
foreach (var keyValue in leftKeys.EntityKeyValues)
5663
{
57-
var leftRelationName = values.GetName(0);
58-
var rightRelationName = values.GetName(1);
64+
var value = keyValue.Value;
5965

60-
foreach (var keyValue in leftKeys.EntityKeyValues)
66+
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
6167
{
62-
var value = keyValue.Value;
68+
value = null;
69+
}
6370

64-
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
65-
{
66-
value = null;
67-
}
71+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
72+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, value)) :
73+
new AuditEntryProperty();
6874

69-
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
70-
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, value)) :
71-
new AuditEntryProperty();
75+
auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, value);
76+
entry.Properties.Add(auditEntryProperty);
77+
}
7278

73-
auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, value);
74-
entry.Properties.Add(auditEntryProperty);
75-
}
79+
foreach (var keyValue in rightKeys.EntityKeyValues)
80+
{
81+
var value = keyValue.Value;
7682

77-
foreach (var keyValue in rightKeys.EntityKeyValues)
83+
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
7884
{
79-
var value = keyValue.Value;
80-
81-
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
82-
{
83-
value = null;
84-
}
85+
value = null;
86+
}
8587

86-
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
87-
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, value)) :
88-
new AuditEntryProperty();
88+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
89+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, value)) :
90+
new AuditEntryProperty();
8991

90-
auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, value);
91-
entry.Properties.Add(auditEntryProperty);
92-
}
92+
auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, value);
93+
entry.Properties.Add(auditEntryProperty);
9394
}
94-
95-
audit.Entries.Add(entry);
9695
}
9796
}
9897
}

src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipDeleted.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,22 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
4141
entry.Build(audit, objectStateEntry);
4242
entry.State = AuditEntryState.RelationshipDeleted;
4343

44+
audit.Entries.Add(entry);
45+
46+
AuditRelationDeleted(audit, entry, objectStateEntry);
47+
}
48+
49+
#if EF5 || EF6
50+
public static void AuditRelationDeleted(Audit audit, AuditEntry entry, ObjectStateEntry objectStateEntry)
51+
#elif EFCORE
52+
public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry)
53+
#endif
54+
{
4455
var values = objectStateEntry.OriginalValues;
4556
for (var i = 0; i < values.FieldCount; i++)
4657
{
4758
var relationName = values.GetName(i);
48-
var entityKey = (EntityKey) values.GetValue(i);
59+
var entityKey = (EntityKey)values.GetValue(i);
4960
foreach (var keyValue in entityKey.EntityKeyValues)
5061
{
5162
var value = keyValue.Value;
@@ -63,8 +74,6 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
6374
entry.Properties.Add(auditEntryProperty);
6475
}
6576
}
66-
67-
audit.Entries.Add(entry);
6877
}
6978
}
7079
}

0 commit comments

Comments
 (0)