Skip to content

Commit 756378c

Browse files
author
zzzprojects
committed
Add audit Where
Add audit Where
1 parent be073c9 commit 756378c

File tree

147 files changed

+3453
-153
lines changed

Some content is hidden

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

147 files changed

+3453
-153
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if EF5 || EF6
9+
using System;
10+
using System.Data.Entity;
11+
using System.Linq;
12+
13+
#elif EFCORE
14+
using System;
15+
using System.Linq;
16+
using System.Reflection;
17+
using Microsoft.EntityFrameworkCore;
18+
19+
#endif
20+
21+
namespace Z.EntityFramework.Plus
22+
{
23+
public static partial class AuditExtensions
24+
{
25+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, T entry) where T : class
26+
{
27+
var context = set.GetDbContext();
28+
var keyNames = context.GetKeyNames<T>();
29+
30+
if (entry == null)
31+
{
32+
return set.Where(x => false);
33+
}
34+
35+
var query = set.Where(x => x.EntityTypeName == typeof(T).Name);
36+
37+
foreach (var keyName in keyNames)
38+
{
39+
var property = entry.GetType().GetProperty(keyName);
40+
var value = property.GetValue(entry, null).ToString();
41+
42+
query = query.Where(x => x.Properties.Any(y => y.PropertyName == property.Name && y.NewValueFormatted == value));
43+
}
44+
45+
query = query.Include(x => x.Properties).OrderBy(x => x.CreatedDate);
46+
47+
return query;
48+
}
49+
50+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, params object[] keyValues) where T : class
51+
{
52+
var context = set.GetDbContext();
53+
var keyNames = context.GetKeyNames<T>();
54+
55+
var query = set.Where(x => x.EntityTypeName == typeof(T).Name);
56+
57+
if (keyValues == null)
58+
{
59+
throw new Exception(ExceptionMessage.Audit_Key_Null);
60+
}
61+
if (keyValues.Length != keyNames.Length)
62+
{
63+
throw new Exception(ExceptionMessage.Audit_Key_OutOfBound);
64+
}
65+
66+
for (var i = 0; i < keyNames.Length; i++)
67+
{
68+
var propertyName = keyNames[i];
69+
var value = keyValues[i] != null ? keyValues[i].ToString() : "";
70+
71+
query = query.Where(x => x.Properties.Any(y => y.PropertyName == propertyName && y.NewValueFormatted == value));
72+
}
73+
74+
query = query.Include(x => x.Properties).OrderBy(x => x.CreatedDate);
75+
76+
return query;
77+
}
78+
}
79+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if EF5 || EF6
9+
using System.Data.Entity;
10+
11+
#elif EFCORE
12+
using Microsoft.Data.Entity;
13+
14+
#endif
15+
16+
namespace Z.EntityFramework.Plus
17+
{
18+
public static partial class AuditExtensions
19+
{
20+
/// <summary>Audits and saves all changes made in this context to the underlying database.</summary>
21+
/// <param name="context">The context used to audits and saves all changes made.</param>
22+
/// <param name="audit">The audit to use to add changes made to the context.</param>
23+
/// <returns>The number of objects written to the underlying database.</returns>-
24+
public static int SaveChanges(this DbContext context, Audit audit)
25+
{
26+
audit.PreSaveChanges(context);
27+
var rowAffecteds = context.SaveChanges();
28+
audit.PostSaveChanges();
29+
30+
if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null)
31+
{
32+
audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit);
33+
context.SaveChanges();
34+
}
35+
36+
return rowAffecteds;
37+
}
38+
}
39+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ internal class ExceptionMessage
1414
#if FULL
1515
public static string BatchOperations_MaxKeyColumns = "Oops! Batch operation only support table with primary keys containing 5 columns or less. For more information, contact us: [email protected]";
1616
#endif
17+
#if FULL || AUDIT
18+
public static string Audit_DbSet_NotFound = "Oops! The audit DbSet cannot be found. Please refer to the documentation to learn how to add a custom audit DbSet. Please report the issue including the stack trace to our support team: [email protected]";
19+
public static string Audit_Key_Null = "Oops! A key must be specified. Please report the issue including the stack trace to our support team: [email protected]";
20+
public static string Audit_Key_OutOfBound = "Oops! The number of argument for the key specified doesn't match with the number of key members. Please report the issue including the stack trace to our support team: [email protected]";
21+
#endif
1722
#if FULL || BATCH_DELETE || BATCH_UPDATE
1823
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
24+
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
1925
#endif
2026
#if FULL || QUERY_INCLUDEFILTER
2127
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

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.3.10")]
22-
[assembly: AssemblyFileVersion("1.3.10")]
21+
[assembly: AssemblyVersion("1.3.11")]
22+
[assembly: AssemblyFileVersion("1.3.11")]

src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="Audit\Audit\PreSaveChanges.cs" />
7777
<Compile Include="Audit\Extensions\DbContext\SaveChanges.cs" />
7878
<Compile Include="Audit\Extensions\DbContext\SaveChangesAsync.cs" />
79+
<Compile Include="Audit\Extensions\DbSet`AuditEntry\DbSet`AuditEntry.cs" />
7980
<Compile Include="BatchDelete\BatchDelete.cs" />
8081
<Compile Include="BatchDelete\BatchDeleteManager.cs" />
8182
<Compile Include="BatchDelete\Extensions\Delete.cs" />
@@ -148,6 +149,7 @@
148149
<Compile Include="_Internal\EF5\IDbSet`\DbContext.AddRange.cs" />
149150
<Compile Include="_Internal\EF5\IDbSet`\DbContext.RemoveRange.cs" />
150151
<Compile Include="_Internal\EF5_EF6\Database\GetEntityConnection.cs" />
152+
<Compile Include="_Internal\EF5_EF6\DbContext\DbContext.GetKeyNames.cs" />
151153
<Compile Include="_Internal\EF5_EF6\DbContext\DbContext.GetObjectContext.cs" />
152154
<Compile Include="_Internal\EF5_EF6\EntityConnection\GetDbTransaction.cs" />
153155
<Compile Include="_Internal\EF5_EF6\EntityConnection\GetEntityTransaction.cs" />
@@ -168,6 +170,7 @@
168170
<Compile Include="_Internal\EFCore\CreateEntity\CreateEntityDataReader.cs" />
169171
<Compile Include="_Internal\EFCore\CreateEntity\CreateEntityRelationalConnection.cs" />
170172
<Compile Include="_Internal\EFCore\DbContext\CreateStoreCommand.cs" />
173+
<Compile Include="_Internal\EFCore\DbContext\DbContext.GetKeyNames.cs" />
171174
<Compile Include="_Internal\EFCore\DbSet`\GetDbContext.cs" />
172175
<Compile Include="_Internal\EFCore\GetStateManager.cs" />
173176
<Compile Include="_Internal\EFCore\IQueryable`\CreateCommand.cs" />
@@ -223,6 +226,9 @@
223226
<Content Include="Audit\Extensions\DbContext\SaveChanges.cs.bak">
224227
<DependentUpon>SaveChanges.cs</DependentUpon>
225228
</Content>
229+
<Content Include="Audit\Extensions\DbSet`AuditEntry\SaveChanges - Copy %282%29.cs.bak">
230+
<DependentUpon>DbSet`AuditEntry.cs</DependentUpon>
231+
</Content>
226232
<None Include="packages.config" />
227233
</ItemGroup>
228234
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if FULL || AUDIT
9+
#if EF5 || EF6
10+
11+
using System.Data.Entity;
12+
using System.Data.Entity.Infrastructure;
13+
using System.Linq;
14+
15+
namespace Z.EntityFramework.Plus
16+
{
17+
public static partial class DbContextExtensions
18+
{
19+
public static string[] GetKeyNames<T>(this DbContext context) where T : class
20+
{
21+
var objectSet = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet<T>();
22+
var keyNames = objectSet.EntitySet.ElementType.KeyMembers
23+
.Select(k => k.Name)
24+
.ToArray();
25+
return keyNames;
26+
}
27+
}
28+
}
29+
30+
#endif
31+
#endif
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if FULL || AUDIT
9+
#if EFCORE
10+
11+
using System.Linq;
12+
using Microsoft.EntityFrameworkCore;
13+
14+
namespace Z.EntityFramework.Plus
15+
{
16+
public static class DbContextExtensions
17+
{
18+
public static string[] GetKeyNames<T>(this DbContext context) where T : class
19+
{
20+
var entityType = context.Model.FindEntityType(typeof (T));
21+
var keys = entityType.GetKeys();
22+
23+
return keys.SelectMany(x => x.Properties.Select(y => y.Name)).ToArray();
24+
}
25+
}
26+
}
27+
28+
#endif
29+
#endif
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if EF5 || EF6
9+
using System;
10+
using System.Data.Entity;
11+
using System.Linq;
12+
13+
#elif EFCORE
14+
using System;
15+
using System.Linq;
16+
using System.Reflection;
17+
using Microsoft.EntityFrameworkCore;
18+
19+
#endif
20+
21+
namespace Z.EntityFramework.Plus
22+
{
23+
public static partial class AuditExtensions
24+
{
25+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, T entry) where T : class
26+
{
27+
var context = set.GetDbContext();
28+
var keyNames = context.GetKeyNames<T>();
29+
30+
if (entry == null)
31+
{
32+
return set.Where(x => false);
33+
}
34+
35+
var query = set.Where(x => x.EntityTypeName == typeof(T).Name);
36+
37+
foreach (var keyName in keyNames)
38+
{
39+
var property = entry.GetType().GetProperty(keyName);
40+
var value = property.GetValue(entry, null).ToString();
41+
42+
query = query.Where(x => x.Properties.Any(y => y.PropertyName == property.Name && y.NewValueFormatted == value));
43+
}
44+
45+
query = query.Include(x => x.Properties).OrderBy(x => x.CreatedDate);
46+
47+
return query;
48+
}
49+
50+
public static IQueryable<AuditEntry> Where<T>(this DbSet<AuditEntry> set, params object[] keyValues) where T : class
51+
{
52+
var context = set.GetDbContext();
53+
var keyNames = context.GetKeyNames<T>();
54+
55+
var query = set.Where(x => x.EntityTypeName == typeof(T).Name);
56+
57+
if (keyValues == null)
58+
{
59+
throw new Exception(ExceptionMessage.Audit_Key_Null);
60+
}
61+
if (keyValues.Length != keyNames.Length)
62+
{
63+
throw new Exception(ExceptionMessage.Audit_Key_OutOfBound);
64+
}
65+
66+
for (var i = 0; i < keyNames.Length; i++)
67+
{
68+
var propertyName = keyNames[i];
69+
var value = keyValues[i] != null ? keyValues[i].ToString() : "";
70+
71+
query = query.Where(x => x.Properties.Any(y => y.PropertyName == propertyName && y.NewValueFormatted == value));
72+
}
73+
74+
query = query.Include(x => x.Properties).OrderBy(x => x.CreatedDate);
75+
76+
return query;
77+
}
78+
}
79+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if EF5 || EF6
9+
using System.Data.Entity;
10+
11+
#elif EFCORE
12+
using Microsoft.Data.Entity;
13+
14+
#endif
15+
16+
namespace Z.EntityFramework.Plus
17+
{
18+
public static partial class AuditExtensions
19+
{
20+
/// <summary>Audits and saves all changes made in this context to the underlying database.</summary>
21+
/// <param name="context">The context used to audits and saves all changes made.</param>
22+
/// <param name="audit">The audit to use to add changes made to the context.</param>
23+
/// <returns>The number of objects written to the underlying database.</returns>-
24+
public static int SaveChanges(this DbContext context, Audit audit)
25+
{
26+
audit.PreSaveChanges(context);
27+
var rowAffecteds = context.SaveChanges();
28+
audit.PostSaveChanges();
29+
30+
if (audit.CurrentOrDefaultConfiguration.AutoSavePreAction != null)
31+
{
32+
audit.CurrentOrDefaultConfiguration.AutoSavePreAction(context, audit);
33+
context.SaveChanges();
34+
}
35+
36+
return rowAffecteds;
37+
}
38+
}
39+
}

src/Z.EntityFramework.Plus.EF5/ExceptionMessage.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ internal class ExceptionMessage
1414
#if FULL
1515
public static string BatchOperations_MaxKeyColumns = "Oops! Batch operation only support table with primary keys containing 5 columns or less. For more information, contact us: [email protected]";
1616
#endif
17+
#if FULL || AUDIT
18+
public static string Audit_DbSet_NotFound = "Oops! The audit DbSet cannot be found. Please refer to the documentation to learn how to add a custom audit DbSet. Please report the issue including the stack trace to our support team: [email protected]";
19+
public static string Audit_Key_Null = "Oops! A key must be specified. Please report the issue including the stack trace to our support team: [email protected]";
20+
public static string Audit_Key_OutOfBound = "Oops! The number of argument for the key specified doesn't match with the number of key members. Please report the issue including the stack trace to our support team: [email protected]";
21+
#endif
1722
#if FULL || BATCH_DELETE || BATCH_UPDATE
1823
public static string BatchOperations_PropertyNotFound = "Oops! Mapping for the property '{0}' cannot be found, the current version don't support yet Complex Type, Enum, TPC, TPH and TPT. For more information, contact us: [email protected]";
24+
public static string BatchOperations_AssemblyNotFound = "Oops! The assembly 'Microsoft.EntityFrameworkCore.SqlServer' could not be found. This feature is only supported for SQL Server for .NET Core. For more information, contact us: [email protected]";
1925
#endif
2026
#if FULL || QUERY_INCLUDEFILTER
2127
public static string QueryIncludeFilter_ArgumentExpression = "Oops! immediate method with expression argument are not supported in EF+ Query IncludeFilter. For more information, contact us: [email protected]";

0 commit comments

Comments
 (0)