Skip to content

Commit 6795c71

Browse files
author
zzzprojects
committed
Update to v1.4.0
Update to v1.4.0
1 parent 4842bdf commit 6795c71

File tree

303 files changed

+14270
-645
lines changed

Some content is hidden

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

303 files changed

+14270
-645
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ public Audit()
2828
{
2929
_configuration = new Lazy<AuditConfiguration>(() => AuditManager.DefaultConfiguration.Clone());
3030
Entries = new List<AuditEntry>();
31+
32+
try
33+
{
34+
#if !NETSTANDARD1_3
35+
CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
36+
#endif
37+
38+
if (string.IsNullOrEmpty(CreatedBy))
39+
{
40+
CreatedBy = "System";
41+
}
42+
}
43+
catch (Exception)
44+
{
45+
// Oops! it's k, this is the responsability of the user to set the default CreatedBy field
46+
CreatedBy = "System";
47+
}
3148
}
3249

3350
/// <summary>Gets or sets the entries.</summary>

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77

88
#if EF5
99
using System.Data.Objects;
10+
using System.Linq;
1011

1112
#elif EF6
1213
using System.Data.Entity.Core.Objects;
14+
using System.Linq;
1315

1416
#elif EFCORE
17+
using Microsoft.EntityFrameworkCore;
1518
using Microsoft.EntityFrameworkCore.ChangeTracking;
1619

1720
#endif
@@ -29,19 +32,20 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt
2932
public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
3033
#endif
3134
{
32-
var entry = new AuditEntry(audit, objectStateEntry)
33-
{
34-
State = AuditEntryState.EntityAdded
35-
};
35+
var entry = audit.Configuration.AuditEntryFactory != null ?
36+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) :
37+
new AuditEntry();
3638

39+
entry.Build(audit, objectStateEntry);
40+
entry.State = AuditEntryState.EntityAdded;
3741

3842
// CHECK if the key should be resolved in POST Action
3943
#if EF5 || EF6
4044
if (objectStateEntry.EntityKey.IsTemporary)
4145
{
4246
entry.DelayedKey = objectStateEntry;
4347
}
44-
AuditEntityAdded(entry, objectStateEntry.CurrentValues);
48+
AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues);
4549
#elif EFCORE
4650
// TODO: We must check if the key IsTemporary! We can maybe use flag...
4751
//if (!objectStateEntry.IsKeySet)
@@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry)
5761
#if EF5 || EF6
5862
/// <summary>Audit entity added.</summary>
5963
/// <param name="auditEntry">The audit entry.</param>
64+
/// <param name="objectStateEntry">The object state entry.</param>
6065
/// <param name="record">The record.</param>
6166
/// <param name="prefix">The prefix.</param>
62-
public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "")
67+
public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "")
6368
{
6469
for (var i = 0; i < record.FieldCount; i++)
6570
{
@@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord
7075
if (valueRecord != null)
7176
{
7277
// Complex Type
73-
AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, "."));
78+
AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, "."));
7479
}
75-
else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
80+
else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
7681
{
77-
auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value));
82+
var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ?
83+
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
84+
new AuditEntryProperty();
85+
86+
auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
87+
auditEntry.Properties.Add(auditEntryProperty);
7888
}
7989
}
8090
}
@@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt
8797
{
8898
var property = objectStateEntry.Property(propertyEntry.Name);
8999

90-
if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
100+
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
91101
{
92-
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue));
102+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
103+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) :
104+
new AuditEntryProperty();
105+
106+
auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue);
107+
entry.Properties.Add(auditEntryProperty);
93108
}
94109
}
95110
}

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System.Data.Common;
9+
using System.Linq;
10+
911
#if EF5
1012
using System.Data.Objects;
1113

1214
#elif EF6
1315
using System.Data.Entity.Core.Objects;
1416

1517
#elif EFCORE
18+
using Microsoft.EntityFrameworkCore;
1619
using Microsoft.EntityFrameworkCore.ChangeTracking;
1720

1821
#endif
@@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE
3033
public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
3134
#endif
3235
{
33-
var entry = new AuditEntry(audit, objectStateEntry)
34-
{
35-
State = AuditEntryState.EntityDeleted
36-
};
36+
var entry = audit.Configuration.AuditEntryFactory != null ?
37+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) :
38+
new AuditEntry();
39+
40+
entry.Build(audit, objectStateEntry);
41+
entry.State = AuditEntryState.EntityDeleted;
3742

3843

3944
#if EF5 || EF6
40-
AuditEntityDeleted(entry, objectStateEntry.OriginalValues);
45+
AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues);
4146
#elif EFCORE
4247
AuditEntityDeleted(entry, objectStateEntry);
4348
#endif
@@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry)
4954
/// <param name="entry">The entry.</param>
5055
/// <param name="record">The record.</param>
5156
/// <param name="prefix">The prefix.</param>
52-
public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "")
57+
public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "")
5358
{
5459
for (var i = 0; i < record.FieldCount; i++)
5560
{
@@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str
6065
if (valueRecord != null)
6166
{
6267
// Complex Type
63-
AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, "."));
68+
AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, "."));
6469
}
65-
else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
70+
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
6671
{
67-
entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null));
72+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
73+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) :
74+
new AuditEntryProperty();
75+
76+
auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null);
77+
entry.Properties.Add(auditEntryProperty);
6878
}
6979
}
7080
}
@@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE
7787
{
7888
var property = objectStateEntry.Property(propertyEntry.Name);
7989

80-
if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
90+
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
8191
{
82-
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null));
92+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
93+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) :
94+
new AuditEntryProperty();
95+
96+
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null);
97+
entry.Properties.Add(auditEntryProperty);
8398
}
8499
}
85100
}

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState
3232
public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state)
3333
#endif
3434
{
35-
var entry = new AuditEntry(audit, objectStateEntry)
36-
{
37-
State = state
38-
};
35+
var entry = audit.Configuration.AuditEntryFactory != null ?
36+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) :
37+
new AuditEntry();
38+
39+
entry.Build(audit, objectStateEntry);
40+
entry.State = state;
3941

4042
#if EF5 || EF6
4143
AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues);
@@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
6870
AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, "."));
6971
}
7072

71-
else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
73+
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
74+
|| entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name))
7275
{
7376
if (!audit.Configuration.IgnorePropertyUnchanged
7477
|| objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
7578
|| !Equals(currentValue, originalValue))
7679
{
77-
entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue));
80+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
81+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) :
82+
new AuditEntryProperty();
83+
84+
auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue);
85+
entry.Properties.Add(auditEntryProperty);
7886
}
7987
}
8088
}
@@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr
8896
{
8997
var property = objectStateEntry.Property(propertyEntry.Name);
9098

91-
if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
99+
if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name))
92100
{
93101
if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified)
94102
{
95-
entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue));
103+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
104+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) :
105+
new AuditEntryProperty();
106+
107+
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue);
108+
entry.Properties.Add(auditEntryProperty);
96109
}
97110
}
98111
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE
3333
public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
3434
#endif
3535
{
36-
var entry = new AuditEntry(audit, objectStateEntry)
37-
{
38-
State = AuditEntryState.RelationshipAdded
39-
};
36+
var entry = audit.Configuration.AuditEntryFactory != null ?
37+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) :
38+
new AuditEntry();
39+
40+
entry.Build(audit, objectStateEntry);
41+
entry.State = AuditEntryState.RelationshipAdded;
4042

4143
var values = objectStateEntry.CurrentValues;
4244

@@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
5557

5658
foreach (var keyValue in leftKeys.EntityKeyValues)
5759
{
58-
entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value));
60+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
61+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) :
62+
new AuditEntryProperty();
63+
64+
auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value);
65+
entry.Properties.Add(auditEntryProperty);
5966
}
6067

6168
foreach (var keyValue in rightKeys.EntityKeyValues)
6269
{
63-
entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value));
70+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
71+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) :
72+
new AuditEntryProperty();
73+
74+
auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value);
75+
entry.Properties.Add(auditEntryProperty);
6476
}
6577
}
6678

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat
3232
public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry)
3333
#endif
3434
{
35-
var entry = new AuditEntry(audit, objectStateEntry)
36-
{
37-
State = AuditEntryState.RelationshipDeleted
38-
};
35+
var entry = audit.Configuration.AuditEntryFactory != null ?
36+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) :
37+
new AuditEntry();
38+
39+
entry.Build(audit, objectStateEntry);
40+
entry.State = AuditEntryState.RelationshipDeleted;
3941

4042
var values = objectStateEntry.OriginalValues;
4143
for (var i = 0; i < values.FieldCount; i++)
@@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
4446
var value = (EntityKey) values.GetValue(i);
4547
foreach (var keyValue in value.EntityKeyValues)
4648
{
47-
entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null));
49+
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
50+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) :
51+
new AuditEntryProperty();
52+
53+
auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null);
54+
entry.Properties.Add(auditEntryProperty);
4855
}
4956
}
5057

0 commit comments

Comments
 (0)