Skip to content

Commit 5220b68

Browse files
Update source
1 parent dbb2b69 commit 5220b68

File tree

5 files changed

+82
-10
lines changed

5 files changed

+82
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Audit()
3939
try
4040
{
4141
#if !NETSTANDARD1_3
42-
CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name;
42+
CreatedBy = System.Threading.Thread.CurrentPrincipal?.Identity?.Name;
4343
#endif
4444

4545
if (string.IsNullOrEmpty(CreatedBy))

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

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,17 @@ public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry
4545
#elif EFCORE
4646
AuditEntityModified(audit, entry, objectStateEntry);
4747
#endif
48-
audit.Entries.Add(entry);
48+
if (audit.Configuration.IgnoreEntityUnchanged)
49+
{
50+
if (entry.Properties != null && entry.Properties.Count > 0)
51+
{
52+
audit.Entries.Add(entry);
53+
}
54+
}
55+
else
56+
{
57+
audit.Entries.Add(entry);
58+
}
4959
}
5060

5161
#if EF5 || EF6
@@ -58,6 +68,8 @@ public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry
5868
/// <param name="prefix">The prefix.</param>
5969
public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord orginalRecord, DbUpdatableDataRecord currentRecord, string prefix = "")
6070
{
71+
bool hasModified = false;
72+
6173
for (var i = 0; i < orginalRecord.FieldCount; i++)
6274
{
6375
var name = orginalRecord.GetName(i);
@@ -84,8 +96,10 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
8496
else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
8597
|| entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, string.Concat(prefix, name)))
8698
{
99+
var isKey = objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name);
100+
87101
if (!audit.Configuration.IgnorePropertyUnchanged
88-
|| objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name)
102+
|| isKey
89103
|| !Equals(currentValue, originalValue))
90104
{
91105
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
@@ -94,15 +108,30 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
94108

95109
auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentRecord, i);
96110
entry.Properties.Add(auditEntryProperty);
111+
112+
if (!isKey)
113+
{
114+
hasModified = true;
115+
}
97116
}
98117
}
99118
}
119+
120+
if (audit.Configuration.IgnoreEntityUnchanged)
121+
{
122+
if (!hasModified)
123+
{
124+
entry.Properties.Clear();
125+
}
126+
}
100127
}
101128
#elif EFCORE
102129
/// <summary>Audit entity modified.</summary>
103130
/// <param name="objectStateEntry">The object state entry.</param>
104131
public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntry objectStateEntry)
105132
{
133+
bool hasModified = false;
134+
106135
foreach (var propertyEntry in objectStateEntry.Metadata.GetProperties())
107136
{
108137
var property = objectStateEntry.Property(propertyEntry.Name);
@@ -117,8 +146,21 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr
117146

118147
auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property);
119148
entry.Properties.Add(auditEntryProperty);
149+
150+
if (property.IsModified)
151+
{
152+
hasModified = true;
153+
}
120154
}
121155
}
156+
}
157+
158+
if (audit.Configuration.IgnoreEntityUnchanged)
159+
{
160+
if (!hasModified)
161+
{
162+
entry.Properties.Clear();
163+
}
122164
}
123165
}
124166
#endif

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,13 @@ public AuditConfiguration()
115115
/// <value>true if the property for entity with Added state are audited, false if not.</value>
116116
public bool IgnorePropertyAdded { get; set; }
117117

118-
/// <summary>Gets or sets a dictionary indicating if an entity type or a property name is audited.</summary>
119-
/// <value>A dictionary indicating if an entity type or a property name is audited.</value>
120-
public ConcurrentDictionary<string, bool> IsAuditedDictionary { get; set; }
118+
/// <summary>Gets or sets a value indicating whether an entity with no modified property is audited.</summary>
119+
/// <value>true if an entity with no modified property should be ignored.</value>
120+
public bool IgnoreEntityUnchanged { get; set; }
121+
122+
/// <summary>Gets or sets a dictionary indicating if an entity type or a property name is audited.</summary>
123+
/// <value>A dictionary indicating if an entity type or a property name is audited.</value>
124+
public ConcurrentDictionary<string, bool> IsAuditedDictionary { get; set; }
121125

122126
/// <summary>Gets or sets a list of predicates to check if the modified entity is soft added.</summary>
123127
/// <value>A list of predicates to check if the modified entity is soft added.</value>
@@ -178,6 +182,7 @@ public AuditConfiguration Clone()
178182
ExcludeRelationshipIfOneExcluded = ExcludeRelationshipIfOneExcluded,
179183
UseUtcDateTime = UseUtcDateTime,
180184
IgnorePropertyAdded = IgnorePropertyAdded,
185+
IgnoreEntityUnchanged = IgnoreEntityUnchanged,
181186
#if EF5 || EF6
182187
UseNullForDBNullValue = UseNullForDBNullValue
183188
#endif

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureBatch.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
using Microsoft.EntityFrameworkCore.Infrastructure;
2929
using Microsoft.EntityFrameworkCore.Query;
3030
using Microsoft.EntityFrameworkCore.Storage;
31+
using Microsoft.EntityFrameworkCore.Storage.Internal;
3132

3233
#endif
3334

@@ -403,8 +404,13 @@ protected DbCommand CreateCommandCombined()
403404
var sql = queryCommand.CommandText;
404405
var parameters = queryCommand.Parameters;
405406

407+
if (parameters.Count == 1 && parameters[0] is CompositeRelationalParameter compositeRelationalParameter)
408+
{
409+
parameters = compositeRelationalParameter.RelationalParameters;
410+
}
411+
406412
// UPDATE parameter name
407-
foreach (var relationalParameter in queryCommand.Parameters)
413+
foreach (var relationalParameter in parameters)
408414
{
409415
var parameter = queryContext.ParameterValues[relationalParameter.InvariantName];
410416

src/shared/Z.EF.Plus.QueryFuture.Shared/QueryFutureValue.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
#elif EF6
2020
using System.Data.Entity.Core.Objects;
21-
#if NET45
21+
#if NET45
2222
using System.Data.Entity.Infrastructure;
2323
#endif
2424

@@ -135,7 +135,17 @@ public override void ExecuteInMemory()
135135
{
136136
#if EFCORE
137137
var query = (IQueryable<TResult>)Query;
138-
var value = query.Provider.Execute<object>(query.Expression);
138+
139+
object value = null;
140+
141+
if (!typeof(TResult).IsPrimitive)
142+
{
143+
value = query.Provider.Execute<object>(query.Expression);
144+
}
145+
else
146+
{
147+
value = query.Provider.Execute<TResult>(query.Expression);
148+
}
139149

140150
if (value is TResult valueTResult)
141151
{
@@ -175,7 +185,16 @@ public override void GetResultDirectly()
175185
{
176186
var query = (IQueryable<TResult>) Query;
177187
#if EFCORE_3X
178-
var value = query.Provider.Execute<object>(query.Expression);
188+
object value = null;
189+
190+
if (!typeof(TResult).IsPrimitive)
191+
{
192+
value = query.Provider.Execute<object>(query.Expression);
193+
}
194+
else
195+
{
196+
value = query.Provider.Execute<TResult>(query.Expression);
197+
}
179198

180199
if (value is TResult valueTResult)
181200
{

0 commit comments

Comments
 (0)