Skip to content

Commit c5b015b

Browse files
author
zzzprojects
committed
Fix Audit DBNull.Value && Query Filter with parameter
Fix Audit DBNull.Value && Query Filter with parameter
1 parent 6f1cb08 commit c5b015b

File tree

58 files changed

+1414
-96
lines changed

Some content is hidden

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

58 files changed

+1414
-96
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
#if EF5
9+
using System;
910
using System.Data.Objects;
1011
using System.Linq;
1112

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

@@ -71,6 +73,11 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
7173
var name = record.GetName(i);
7274
var value = record.GetValue(i);
7375

76+
if (auditEntry.Parent.Configuration.UseNullForDBNullValue && value == DBNull.Value)
77+
{
78+
value = null;
79+
}
80+
7481
var valueRecord = value as DbUpdatableDataRecord;
7582
if (valueRecord != null)
7683
{
@@ -80,8 +87,8 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
8087
else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
8188
{
8289
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();
90+
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
91+
new AuditEntryProperty();
8592

8693
auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
8794
auditEntry.Properties.Add(auditEntryProperty);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.Data.Common;
910
using System.Linq;
1011

@@ -61,6 +62,11 @@ public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectS
6162
var name = record.GetName(i);
6263
var value = record.GetValue(i);
6364

65+
if (entry.Parent.Configuration.UseNullForDBNullValue && value == DBNull.Value)
66+
{
67+
value = null;
68+
}
69+
6470
var valueRecord = value as DbDataRecord;
6571
if (valueRecord != null)
6672
{

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.Data.Common;
910
using System.Linq;
1011
#if EF5
@@ -63,6 +64,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat
6364
var originalValue = orginalRecord.GetValue(i);
6465
var currentValue = currentRecord.GetValue(i);
6566

67+
if (audit.Configuration.UseNullForDBNullValue && originalValue == DBNull.Value)
68+
{
69+
originalValue = null;
70+
}
71+
72+
if (audit.Configuration.UseNullForDBNullValue && currentValue == DBNull.Value)
73+
{
74+
currentValue = null;
75+
}
76+
6677
var valueRecord = originalValue as DbDataRecord;
6778
if (valueRecord != null)
6879
{

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
#if EF5 || EF6
99

1010
#if EF5
11+
using System;
1112
using System.Data;
1213
using System.Data.Objects;
1314

1415
#elif EF6
16+
using System;
1517
using System.Data.Entity.Core;
1618
using System.Data.Entity.Core.Objects;
1719

@@ -57,21 +59,35 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry)
5759

5860
foreach (var keyValue in leftKeys.EntityKeyValues)
5961
{
62+
var value = keyValue.Value;
63+
64+
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
65+
{
66+
value = null;
67+
}
68+
6069
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
61-
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) :
70+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, value)) :
6271
new AuditEntryProperty();
6372

64-
auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value);
73+
auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, value);
6574
entry.Properties.Add(auditEntryProperty);
6675
}
6776

6877
foreach (var keyValue in rightKeys.EntityKeyValues)
6978
{
79+
var value = keyValue.Value;
80+
81+
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
82+
{
83+
value = null;
84+
}
85+
7086
var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ?
71-
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) :
87+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, value)) :
7288
new AuditEntryProperty();
7389

74-
auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value);
90+
auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, value);
7591
entry.Properties.Add(auditEntryProperty);
7692
}
7793
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
#if EF5 || EF6
99
#if EF5
10+
using System;
1011
using System.Data;
1112
using System.Data.Objects;
1213

1314
#elif EF6
15+
using System;
1416
using System.Data.Entity.Core;
1517
using System.Data.Entity.Core.Objects;
1618

@@ -33,8 +35,8 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
3335
#endif
3436
{
3537
var entry = audit.Configuration.AuditEntryFactory != null ?
36-
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) :
37-
new AuditEntry();
38+
audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) :
39+
new AuditEntry();
3840

3941
entry.Build(audit, objectStateEntry);
4042
entry.State = AuditEntryState.RelationshipDeleted;
@@ -43,14 +45,21 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr
4345
for (var i = 0; i < values.FieldCount; i++)
4446
{
4547
var relationName = values.GetName(i);
46-
var value = (EntityKey) values.GetValue(i);
47-
foreach (var keyValue in value.EntityKeyValues)
48+
var entityKey = (EntityKey) values.GetValue(i);
49+
foreach (var keyValue in entityKey.EntityKeyValues)
4850
{
51+
var value = keyValue.Value;
52+
53+
if (audit.Configuration.UseNullForDBNullValue && value == DBNull.Value)
54+
{
55+
value = null;
56+
}
57+
4958
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();
59+
entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, value, null)) :
60+
new AuditEntryProperty();
5261

53-
auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null);
62+
auditEntryProperty.Build(entry, relationName, keyValue.Key, value, null);
5463
entry.Properties.Add(auditEntryProperty);
5564
}
5665
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
using System;
99
using System.Collections.Concurrent;
1010
using System.Collections.Generic;
11-
1211
#if EF5
1312
using System.Data.Entity;
1413
using System.Data.Objects;
1514

1615
#elif EF6
1716
using System.Data.Entity;
18-
using System.Data.Entity.Core.Objects;
1917

2018
#elif EFCORE
2119
using Microsoft.EntityFrameworkCore;
@@ -42,11 +40,16 @@ public AuditConfiguration()
4240

4341
IsAuditedDictionary = new ConcurrentDictionary<string, bool>();
4442
ValueFormatterDictionary = new ConcurrentDictionary<string, Func<object, object>>();
43+
44+
45+
#if EF5 || EF6
46+
UseNullForDBNullValue = true;
47+
#endif
4548
}
4649

4750
public Func<AuditEntryFactoryArgs, AuditEntry> AuditEntryFactory { get; set; }
48-
49-
public Func<AuditEntryPropertyArgs, AuditEntryProperty> AuditEntryPropertyFactory { get; set; }
51+
52+
public Func<AuditEntryPropertyArgs, AuditEntryProperty> AuditEntryPropertyFactory { get; set; }
5053

5154
/// <summary>Gets or sets the automatic audit save pre action.</summary>
5255
/// <value>The automatic audit save pre action.</value>
@@ -112,6 +115,14 @@ public AuditConfiguration()
112115
/// <value>A dictionary of value formatter for a property name.</value>
113116
public ConcurrentDictionary<string, Func<object, object>> ValueFormatterDictionary { get; set; }
114117

118+
#if EF5 || EF6
119+
/// <summary>
120+
/// Gets or sets a value indicating whether null value should be used in the Audit instead of DBNull.Value
121+
/// </summary>
122+
/// <value>The value indicating whether null value should be used in the Audit instead of DBNull.Value</value>
123+
public bool UseNullForDBNullValue { get; set; }
124+
#endif
125+
115126
/// <summary>Makes a deep copy of this object.</summary>
116127
/// <returns>A copy of this object.</returns>
117128
public AuditConfiguration Clone()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public string FormatValue(EntityEntry entry, string propertyName, object current
6969
}
7070
}
7171

72-
return currentValue != null ? currentValue.ToString() : null;
72+
return currentValue != null && currentValue != DBNull.Value ? currentValue.ToString() : null;
7373
}
7474
}
7575
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8+
using System;
89
using System.ComponentModel.DataAnnotations;
910
using System.ComponentModel.DataAnnotations.Schema;
1011

@@ -108,7 +109,7 @@ public string NewValueFormatted
108109
return Parent.Parent.CurrentOrDefaultConfiguration.FormatValue(Parent.Entry, PropertyName, currentValue);
109110
}
110111

111-
return currentValue != null ? currentValue.ToString() : null;
112+
return currentValue != null && currentValue != DBNull.Value ? currentValue.ToString() : null;
112113
}
113114
set { NewValue = value; }
114115
}
@@ -137,7 +138,7 @@ public string OldValueFormatted
137138
return Parent.Parent.CurrentOrDefaultConfiguration.FormatValue(Parent.Entry, PropertyName, currentValue);
138139
}
139140

140-
return currentValue != null ? currentValue.ToString() : null;
141+
return currentValue != null && currentValue != DBNull.Value ? currentValue.ToString() : null;
141142
}
142143
set { OldValue = value; }
143144
}

src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("e4c2af73-caeb-4429-bcb6-0a359484e064")]
21-
[assembly: AssemblyVersion("1.4.7")]
22-
[assembly: AssemblyFileVersion("1.4.7")]
21+
[assembly: AssemblyVersion("1.4.8")]
22+
[assembly: AssemblyFileVersion("1.4.8")]

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
#if EF5
9+
using System;
910
using System.Data.Objects;
1011
using System.Linq;
1112

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

@@ -71,6 +73,11 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
7173
var name = record.GetName(i);
7274
var value = record.GetValue(i);
7375

76+
if (auditEntry.Parent.Configuration.UseNullForDBNullValue && value == DBNull.Value)
77+
{
78+
value = null;
79+
}
80+
7481
var valueRecord = value as DbUpdatableDataRecord;
7582
if (valueRecord != null)
7683
{
@@ -80,8 +87,8 @@ public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry obje
8087
else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name))
8188
{
8289
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();
90+
auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) :
91+
new AuditEntryProperty();
8592

8693
auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value);
8794
auditEntry.Properties.Add(auditEntryProperty);

0 commit comments

Comments
 (0)