From 6795c717a06fae264424f34851e08f41d31c40f6 Mon Sep 17 00:00:00 2001 From: zzzprojects Date: Tue, 13 Sep 2016 01:02:46 -0400 Subject: [PATCH] Update to v1.4.0 Update to v1.4.0 --- .../Audit/Audit.cs | 17 + .../Audit/Audit/AuditEntityAdded.cs | 37 +- .../Audit/Audit/AuditEntityDeleted.cs | 37 +- .../Audit/Audit/AuditEntityModified.cs | 29 +- .../Audit/Audit/AuditRelationshipAdded.cs | 24 +- .../Audit/Audit/AuditRelationshipDeleted.cs | 17 +- .../Audit/Audit/PostSaveChanges.cs | 18 +- .../Audit/AuditConfiguration.cs | 12 +- .../Audit/AuditEntry.cs | 64 ++- .../Audit/AuditEntryFactoryArgs.cs | 41 ++ .../Audit/AuditEntryProperty.cs | 57 +-- .../Audit/AuditEntryPropertyArgs.cs | 56 +++ .../Properties/AssemblyInfo.cs | 4 +- .../Z.EntityFramework.Plus.EF5.NET40.csproj | 2 + .../DbContext/DbContext.GetObjectContext.cs | 2 +- .../IQueryable/IQueryable.GetObjectQuery.cs | 2 +- .../IQueryable`/IQueryable`.GetDbContext.cs | 2 +- .../IQueryable`/IQueryable`.GetObjectQuery.cs | 32 +- .../EF6/ObjectContext/GetDbContext.cs | 2 +- .../EFCore/IQueryable`/GetDbContext.cs | 2 +- src/Z.EntityFramework.Plus.EF5/Audit/Audit.cs | 17 + .../Audit/Audit/AuditEntityAdded.cs | 37 +- .../Audit/Audit/AuditEntityDeleted.cs | 37 +- .../Audit/Audit/AuditEntityModified.cs | 29 +- .../Audit/Audit/AuditRelationshipAdded.cs | 24 +- .../Audit/Audit/AuditRelationshipDeleted.cs | 17 +- .../Audit/Audit/PostSaveChanges.cs | 18 +- .../Audit/AuditConfiguration.cs | 12 +- .../Audit/AuditEntry.cs | 64 ++- .../Audit/AuditEntryFactoryArgs.cs | 41 ++ .../Audit/AuditEntryProperty.cs | 57 +-- .../Audit/AuditEntryPropertyArgs.cs | 56 +++ .../Properties/AssemblyInfo.cs | 4 +- .../Z.EntityFramework.Plus.EF5.csproj | 2 + .../DbContext/DbContext.GetObjectContext.cs | 2 +- .../IQueryable/IQueryable.GetObjectQuery.cs | 2 +- .../IQueryable`/IQueryable`.GetDbContext.cs | 2 +- .../IQueryable`/IQueryable`.GetObjectQuery.cs | 32 +- .../EF6/ObjectContext/GetDbContext.cs | 2 +- .../EFCore/IQueryable`/GetDbContext.cs | 2 +- .../Audit/Audit.cs | 17 + .../Audit/Audit/AuditEntityAdded.cs | 37 +- .../Audit/Audit/AuditEntityDeleted.cs | 37 +- .../Audit/Audit/AuditEntityModified.cs | 29 +- .../Audit/Audit/AuditRelationshipAdded.cs | 24 +- .../Audit/Audit/AuditRelationshipDeleted.cs | 17 +- .../Audit/Audit/PostSaveChanges.cs | 18 +- .../Audit/AuditConfiguration.cs | 12 +- .../Audit/AuditEntry.cs | 64 ++- .../Audit/AuditEntryFactoryArgs.cs | 41 ++ .../Audit/AuditEntryProperty.cs | 57 +-- .../Audit/AuditEntryPropertyArgs.cs | 56 +++ .../Properties/AssemblyInfo.cs | 4 +- .../QueryFilter/BaseQueryFilter.cs | 4 +- .../QueryFilter/BaseQueryFilterQueryable.cs | 4 +- .../Extensions/DbContext.Filter.cs | 4 +- .../Extensions/IDbSet.AsNoFilter.cs | 4 +- .../QueryFilter/Extensions/IDbSet.Filter.cs | 4 +- .../QueryFilter/QueryFilter.cs | 4 +- .../QueryFilter/QueryFilterContext.cs | 6 +- .../QueryFilter/QueryFilterManager.cs | 4 +- .../QueryFilter/QueryFilterQueryable.cs | 4 +- .../QueryFilter/QueryFilterSet.cs | 4 +- .../BaseQueryFilterInterceptor.cs | 154 +++++++ .../Extensions/DbContext.Filter.cs | 61 +++ .../Extensions/IDbSet.AsNoFilter.cs | 24 ++ .../Extensions/IDbSet.Filter.cs | 50 +++ .../QueryFilterInterceptor.cs | 62 +++ .../QueryFilterInterceptorApply.cs | 37 ++ .../QueryFilterInterceptorContext.cs | 205 ++++++++++ .../QueryFilterInterceptorDbCommandTree.cs | 89 +++++ ...ueryFilterInterceptorDbFilterExpression.cs | 94 +++++ ...eryFilterInterceptorDbProjectExpression.cs | 44 ++ .../QueryFilterInterceptorDbScanExpression.cs | 131 ++++++ .../QueryFilterInterceptorManager.cs | 221 ++++++++++ .../Z.EntityFramework.Plus.EF6.NET40.csproj | 14 + .../DbContext/DbContext.GetObjectContext.cs | 2 +- .../IQueryable/IQueryable.GetObjectQuery.cs | 2 +- .../IQueryable`/IQueryable`.GetDbContext.cs | 2 +- .../IQueryable`/IQueryable`.GetObjectQuery.cs | 32 +- .../EF6/ObjectContext/GetDbContext.cs | 2 +- .../EFCore/IQueryable`/GetDbContext.cs | 2 +- src/Z.EntityFramework.Plus.EF6/Audit/Audit.cs | 17 + .../Audit/Audit/AuditEntityAdded.cs | 37 +- .../Audit/Audit/AuditEntityDeleted.cs | 37 +- .../Audit/Audit/AuditEntityModified.cs | 29 +- .../Audit/Audit/AuditRelationshipAdded.cs | 24 +- .../Audit/Audit/AuditRelationshipDeleted.cs | 17 +- .../Audit/Audit/PostSaveChanges.cs | 18 +- .../Audit/AuditConfiguration.cs | 12 +- .../Audit/AuditEntry.cs | 64 ++- .../Audit/AuditEntryFactoryArgs.cs | 41 ++ .../Audit/AuditEntryProperty.cs | 57 +-- .../Audit/AuditEntryPropertyArgs.cs | 56 +++ .../Properties/AssemblyInfo.cs | 4 +- .../QueryFilter/BaseQueryFilter.cs | 4 +- .../QueryFilter/BaseQueryFilterQueryable.cs | 4 +- .../Extensions/DbContext.Filter.cs | 4 +- .../Extensions/IDbSet.AsNoFilter.cs | 4 +- .../QueryFilter/Extensions/IDbSet.Filter.cs | 4 +- .../QueryFilter/QueryFilter.cs | 4 +- .../QueryFilter/QueryFilterContext.cs | 4 +- .../QueryFilter/QueryFilterManager.cs | 4 +- .../QueryFilter/QueryFilterQueryable.cs | 4 +- .../QueryFilter/QueryFilterSet.cs | 4 +- .../BaseQueryFilterInterceptor.cs | 154 +++++++ .../Extensions/DbContext.Filter.cs | 61 +++ .../Extensions/IDbSet.AsNoFilter.cs | 24 ++ .../Extensions/IDbSet.Filter.cs | 50 +++ .../QueryFilterInterceptor.cs | 62 +++ .../QueryFilterInterceptorApply.cs | 37 ++ .../QueryFilterInterceptorContext.cs | 205 ++++++++++ .../QueryFilterInterceptorDbCommandTree.cs | 89 +++++ ...ueryFilterInterceptorDbFilterExpression.cs | 94 +++++ ...eryFilterInterceptorDbProjectExpression.cs | 44 ++ .../QueryFilterInterceptorDbScanExpression.cs | 131 ++++++ .../QueryFilterInterceptorManager.cs | 221 ++++++++++ .../BaseQueryIncludeOptimizedChild.cs | 3 + .../IQueryable`.IncludeOptimized.cs | 12 + .../QueryIncludeOptimizedChild`2.cs | 5 + .../QueryIncludeOptimizedParentQueryable`.cs | 61 ++- .../Z.EntityFramework.Plus.EF6.csproj | 20 +- .../DbContext/DbContext.GetObjectContext.cs | 2 +- .../IQueryable/IQueryable.GetObjectQuery.cs | 2 +- .../IQueryable`/IQueryable`.GetObjectQuery.cs | 32 +- .../Audit/Audit.cs | 17 + .../Audit/Audit/AuditEntityAdded.cs | 37 +- .../Audit/Audit/AuditEntityDeleted.cs | 37 +- .../Audit/Audit/AuditEntityModified.cs | 29 +- .../Audit/Audit/AuditRelationshipAdded.cs | 24 +- .../Audit/Audit/AuditRelationshipDeleted.cs | 17 +- .../Audit/Audit/PostSaveChanges.cs | 18 +- .../Audit/AuditConfiguration.cs | 12 +- .../Audit/AuditEntry.cs | 64 ++- .../Audit/AuditEntryFactoryArgs.cs | 41 ++ .../Audit/AuditEntryProperty.cs | 57 +-- .../Audit/AuditEntryPropertyArgs.cs | 56 +++ .../Properties/AssemblyInfo.cs | 4 +- .../DbContext/DbContext.GetObjectContext.cs | 2 +- .../IQueryable/IQueryable.GetObjectQuery.cs | 2 +- .../IQueryable`/IQueryable`.GetDbContext.cs | 2 +- .../IQueryable`/IQueryable`.GetObjectQuery.cs | 32 +- .../EF6/ObjectContext/GetDbContext.cs | 2 +- .../EFCore/IQueryable`/GetDbContext.cs | 2 +- .../project.json | 2 +- src/Z.EntityFramework.Plus.sln | 3 +- .../Audit_Configuration_AuditEntryFactory.cs | 16 + ...Configuration_AuditEntryPropertyFactory.cs | 16 + .../Audit/Audit_CreatedBy.cs | 16 + .../EntityAdded.cs | 179 +++++++++ .../EntityDeleted.cs | 181 +++++++++ .../EntityModified.cs | 181 +++++++++ .../EntitySoftAdded.cs | 182 +++++++++ .../EntitySoftDeleted.cs | 182 +++++++++ .../RelationshipAdded.cs | 176 ++++++++ .../RelationshipDeleted.cs | 182 +++++++++ .../EntityAdded.cs | 181 +++++++++ .../EntityDeleted.cs | 183 +++++++++ .../EntityModified.cs | 183 +++++++++ .../EntitySoftAdded.cs | 184 +++++++++ .../EntitySoftDeleted.cs | 184 +++++++++ .../RelationshipAdded.cs | 176 ++++++++ .../RelationshipDeleted.cs | 182 +++++++++ .../Configuration_ExcludeProperty/All.cs | 40 +- .../Configuration_ExcludeProperty/Type.cs | 36 +- .../Configuration_IncludeProperty/Single.cs | 1 + .../Configuration_IncludeProperty/Type.cs | 36 +- .../Audit/CreatedBy/CurrentPrincipal.cs | 179 +++++++++ .../Audit/EntityAdded/Entity_Proxy.cs | 169 ++++++++ .../Audit/EntityDeleted/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntityModified/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntitySoftAdded/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntitySoftDeleted/Entity_Proxy.cs | 186 +++++++++ .../Z.Test.EntityFramework.Plus.EF5.csproj | 31 ++ .../_Helper/AuditHelper.cs | 47 ++- .../_Helper/Extensions/QueryCacheHelper.cs | 26 ++ .../_Helper/QueryCacheHelper.cs | 4 +- .../_Helper/QueryFilterHelper.cs | 14 + .../_Model/AuditEntryProperty_Extended.cs | 9 + .../_Model/AuditEntry_Extended.cs | 9 + .../_Model/Entity_Enum.cs | 20 + .../_Model/Entity_Proxy.cs | 13 + .../_Model/Entity_Proxy_Right.cs | 11 + ...nheritance_Interface_Entity_LazyLoading.cs | 20 + .../_Model/Internal_Entity_Basic.cs | 4 +- .../_Model/Internal_Entity_Basic_Many.cs | 4 +- .../_Model/Methods/DeleteAll.cs | 2 +- .../_Model/Methods/GetIdentitySeed.cs | 2 +- .../_Model/Methods/Insert.cs | 2 +- .../_Model/Methods/TypeName.cs | 2 +- .../_Model/_TestContext.cs | 62 ++- .../_Model/_TestContextMemory.cs | 377 ++++++++++++++++++ .../Audit_Configuration_AuditEntryFactory.cs | 16 + ...Configuration_AuditEntryPropertyFactory.cs | 16 + .../Audit/Audit_CreatedBy.cs | 16 + .../EntityAdded.cs | 179 +++++++++ .../EntityDeleted.cs | 181 +++++++++ .../EntityModified.cs | 181 +++++++++ .../EntitySoftAdded.cs | 182 +++++++++ .../EntitySoftDeleted.cs | 182 +++++++++ .../RelationshipAdded.cs | 176 ++++++++ .../RelationshipDeleted.cs | 182 +++++++++ .../EntityAdded.cs | 181 +++++++++ .../EntityDeleted.cs | 183 +++++++++ .../EntityModified.cs | 183 +++++++++ .../EntitySoftAdded.cs | 184 +++++++++ .../EntitySoftDeleted.cs | 184 +++++++++ .../RelationshipAdded.cs | 176 ++++++++ .../RelationshipDeleted.cs | 182 +++++++++ .../Configuration_ExcludeProperty/All.cs | 40 +- .../Configuration_ExcludeProperty/Type.cs | 36 +- .../Configuration_IncludeProperty/Single.cs | 1 + .../Configuration_IncludeProperty/Type.cs | 36 +- .../Audit/CreatedBy/CurrentPrincipal.cs | 179 +++++++++ .../Audit/EntityAdded/Entity_Proxy.cs | 169 ++++++++ .../Audit/EntityDeleted/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntityModified/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntitySoftAdded/Entity_Proxy.cs | 185 +++++++++ .../Audit/EntitySoftDeleted/Entity_Proxy.cs | 186 +++++++++ .../WithGlobalFilter/ManyFilter_Enable.cs | 3 + .../WithGlobalFilter/ManyFilter_Exclude.cs | 3 + .../WithGlobalFilter/ManyFilter_Include.cs | 3 + .../WithGlobalFilter/SingleFilter_Enable.cs | 3 + .../WithGlobalFilter/SingleFilter_Exclude.cs | 3 + .../WithGlobalFilter/SingleFilter_Include.cs | 3 + .../ManyFilter_Enable.cs | 5 +- .../SingleFilter_Enable.cs | 3 + .../ManyFilter_Enable.cs | 3 + .../ManyFilter_Exclude.cs | 3 + .../ManyFilter_Include.cs | 3 + .../SingleFilter_Enable.cs | 3 + .../SingleFilter_Exclude.cs | 3 + .../SingleFilter_Include.cs | 3 + .../WithInclude/ManyFilter.cs | 41 ++ .../WithInclude/ManyFilter_Disable.cs | 41 ++ .../WithInclude/ManyFilter_Enable.cs | 41 ++ .../WithInclude/ManyFilter_Exclude.cs | 41 ++ .../WithInclude/ManyFilter_Include.cs | 41 ++ .../WithInclude/SingleFilter.cs | 41 ++ .../WithInclude/SingleFilter_Disable.cs | 41 ++ .../WithInclude/SingleFilter_Enable.cs | 41 ++ .../WithInclude/SingleFilter_Exclude.cs | 41 ++ .../WithInclude/SingleFilter_Include.cs | 41 ++ .../WithInstanceFilter/ManyFilter_Enable.cs | 3 + .../WithInstanceFilter/ManyFilter_Exclude.cs | 3 + .../WithInstanceFilter/SingleFilter_Enable.cs | 3 + .../SingleFilter_Exclude.cs | 3 + .../WithLazyLoading/ManyFilter.cs | 41 ++ .../WithLazyLoading/ManyFilter_Disable.cs | 41 ++ .../WithLazyLoading/ManyFilter_Enable.cs | 41 ++ .../WithLazyLoading/ManyFilter_Exclude.cs | 41 ++ .../WithLazyLoading/ManyFilter_Include.cs | 41 ++ .../WithLazyLoading/SingleFilter.cs | 41 ++ .../WithLazyLoading/SingleFilter_Disable.cs | 41 ++ .../WithLazyLoading/SingleFilter_Enable.cs | 41 ++ .../WithLazyLoading/SingleFilter_Exclude.cs | 41 ++ .../WithLazyLoading/SingleFilter_Include.cs | 41 ++ .../WithGlobalFilter/ManyFilter_Enabled.cs | 3 + .../WithGlobalFilter/SingleFilter_Enabled.cs | 3 + ...alFilterDisabled_InstanceFilterDisabled.cs | 3 + ...balFilterDisabled_InstanceFilterEnabled.cs | 3 + ...balFilterEnabled_InstanceFilterDisabled.cs | 3 + ...obalFilterEnabled_InstanceFilterEnabled.cs | 3 + .../ManyFilter_Enabled.cs | 3 + .../SingleFilter_Enabled.cs | 3 + .../WithInclude/ManyFilter_Disabled.cs | 42 ++ .../WithInclude/ManyFilter_Enabled.cs | 42 ++ .../WithInclude/SingleFilter_Disabled.cs | 42 ++ .../WithInclude/SingleFilter_Enabled.cs | 42 ++ .../WithInstanceFilter/ManyFilter_Enabled.cs | 3 + .../SingleFilter_Enabled.cs | 3 + .../WithLazyLoading/ManyFilter_Disabled.cs | 42 ++ .../WithLazyLoading/ManyFilter_Enabled.cs | 44 ++ .../WithLazyLoading/SingleFilter_Disabled.cs | 42 ++ .../WithLazyLoading/SingleFilter_Enabled.cs | 44 ++ .../WithGlobalFilter/ManyFilter_Enabled.cs | 3 + .../WithGlobalFilter/SingleFilter_Enabled.cs | 3 + ...alFilterDisabled_InstanceFilterDisabled.cs | 3 + ...balFilterDisabled_InstanceFilterEnabled.cs | 3 + ...balFilterEnabled_InstanceFilterDisabled.cs | 3 + ...obalFilterEnabled_InstanceFilterEnabled.cs | 3 + .../ManyFilter_Enabled.cs | 3 + .../SingleFilter_Enabled.cs | 3 + .../WithInclude/ManyFilter_Disabled.cs | 47 +++ .../WithInclude/ManyFilter_Enabled.cs | 47 +++ .../WithInclude/SingleFilter_Disabled.cs | 47 +++ .../WithInclude/SingleFilter_Enabled.cs | 47 +++ .../WithInstanceFilter/ManyFilter_Enabled.cs | 3 + .../SingleFilter_Enabled.cs | 3 + .../WithLazyLoading/ManyFilter_Disabled.cs | 48 +++ .../WithLazyLoading/ManyFilter_Enabled.cs | 47 +++ .../WithLazyLoading/SingleFilter_Disabled.cs | 48 +++ .../WithLazyLoading/SingleFilter_Enabled.cs | 47 +++ .../Z.Test.EntityFramework.Plus.EF6.csproj | 64 +++ .../_Helper/AuditHelper.cs | 45 +++ .../_Helper/QueryFilterHelper.cs | 10 + .../_Model/AuditEntryProperty_Extended.cs | 9 + .../_Model/AuditEntry_Extended.cs | 9 + .../_Model/Entity_Proxy.cs | 13 + .../_Model/Entity_Proxy_Right.cs | 11 + ...nheritance_Interface_Entity_LazyLoading.cs | 20 + .../_Model/_TestContext.cs | 28 +- version.txt | 2 +- 303 files changed, 14270 insertions(+), 645 deletions(-) create mode 100644 src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryFactoryArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryPropertyArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryFactoryArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryPropertyArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryFactoryArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryPropertyArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/DbContext.Filter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptor.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorApply.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorContext.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorManager.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryFactoryArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryPropertyArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/DbContext.Filter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptor.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorApply.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorContext.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs create mode 100644 src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorManager.cs create mode 100644 src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryFactoryArgs.cs create mode 100644 src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryPropertyArgs.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryFactory.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_CreatedBy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityModified.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/CreatedBy/CurrentPrincipal.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityAdded/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityDeleted/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityModified/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftAdded/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftDeleted/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/Extensions/QueryCacheHelper.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntryProperty_Extended.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntry_Extended.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Enum.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy_Right.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Inheritance_Interface_Entity_LazyLoading.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContextMemory.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryFactory.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_CreatedBy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityModified.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/CreatedBy/CurrentPrincipal.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityAdded/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityDeleted/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityModified/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftAdded/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftDeleted/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Disable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Enable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Exclude.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Include.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Disable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Enable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Exclude.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Include.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Disable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Enable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Exclude.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Include.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Disable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Enable.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Exclude.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Include.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Disabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Enabled.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntryProperty_Extended.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntry_Extended.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy_Right.cs create mode 100644 src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Inheritance_Interface_Entity_LazyLoading.cs diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit.cs index 4828f3cd..20f57234 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit.cs @@ -28,6 +28,23 @@ public Audit() { _configuration = new Lazy(() => AuditManager.DefaultConfiguration.Clone()); Entries = new List(); + + try + { +#if !NETSTANDARD1_3 + CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; +#endif + + if (string.IsNullOrEmpty(CreatedBy)) + { + CreatedBy = "System"; + } + } + catch (Exception) + { + // Oops! it's k, this is the responsability of the user to set the default CreatedBy field + CreatedBy = "System"; + } } /// Gets or sets the entries. diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityAdded.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityAdded.cs index ce2e47b4..9c0eb54e 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityAdded.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityAdded.cs @@ -7,11 +7,14 @@ #if EF5 using System.Data.Objects; +using System.Linq; #elif EF6 using System.Data.Entity.Core.Objects; +using System.Linq; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -29,11 +32,12 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) : + new AuditEntry(); + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityAdded; // CHECK if the key should be resolved in POST Action #if EF5 || EF6 @@ -41,7 +45,7 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) { entry.DelayedKey = objectStateEntry; } - AuditEntityAdded(entry, objectStateEntry.CurrentValues); + AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues); #elif EFCORE // TODO: We must check if the key IsTemporary! We can maybe use flag... //if (!objectStateEntry.IsKeySet) @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #if EF5 || EF6 /// Audit entity added. /// The audit entry. + /// The object state entry. /// The record. /// The prefix. - public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "") + public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord if (valueRecord != null) { // Complex Type - AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) + else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) { - auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value)); + var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ? + auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value); + auditEntry.Properties.Add(auditEntryProperty); } } } @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityDeleted.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityDeleted.cs index 4505955f..845177b1 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityDeleted.cs @@ -6,6 +6,8 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; +using System.Linq; + #if EF5 using System.Data.Objects; @@ -13,6 +15,7 @@ using System.Data.Entity.Core.Objects; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityDeleted; #if EF5 || EF6 - AuditEntityDeleted(entry, objectStateEntry.OriginalValues); + AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues); #elif EFCORE AuditEntityDeleted(entry, objectStateEntry); #endif @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) /// The entry. /// The record. /// The prefix. - public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "") + public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str if (valueRecord != null) { // Complex Type - AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null); + entry.Properties.Add(auditEntryProperty); } } } @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityModified.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityModified.cs index cfc256c0..af5db628 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityModified.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditEntityModified.cs @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = state - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = state; #if EF5 || EF6 AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues); @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) + || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { if (!audit.Configuration.IgnorePropertyUnchanged || objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || !Equals(currentValue, originalValue)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue); + entry.Properties.Add(auditEntryProperty); } } } @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipAdded.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipAdded.cs index 63f78fd3..7351d739 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipAdded.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipAdded.cs @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipAdded; var values = objectStateEntry.CurrentValues; @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipDeleted.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipDeleted.cs index ec5b633a..fa31bedd 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/AuditRelationshipDeleted.cs @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipDeleted; var values = objectStateEntry.OriginalValues; for (var i = 0; i < values.FieldCount; i++) @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr var value = (EntityKey) values.GetValue(i); foreach (var keyValue in value.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/PostSaveChanges.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/PostSaveChanges.cs index 6ae5cf98..99fe1cdb 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/PostSaveChanges.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/Audit/PostSaveChanges.cs @@ -49,19 +49,29 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } else { foreach (var keyValue in objectStateEntry.EntityKey.EntityKeyValues) { - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Key); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Key); // ENSURE the property is audited if (property != null) @@ -75,7 +85,7 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in objectStateEntry.Metadata.GetKeys()) { var key = objectStateEntry.Property(keyValue.Properties[0].Name); - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Properties[0].Name); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Properties[0].Name); // ENSURE the property is audited if (property != null) diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditConfiguration.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditConfiguration.cs index a4271895..5614cb40 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditConfiguration.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditConfiguration.cs @@ -8,8 +8,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -#if EF5 || EF6 + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Core.Objects; #elif EFCORE using Microsoft.EntityFrameworkCore; @@ -38,6 +44,10 @@ public AuditConfiguration() ValueFormatterDictionary = new ConcurrentDictionary>(); } + public Func AuditEntryFactory { get; set; } + + public Func AuditEntryPropertyFactory { get; set; } + /// Gets or sets the automatic audit save pre action. /// The automatic audit save pre action. public Action AutoSavePreAction { get; set; } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntry.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntry.cs index 00301715..babc1094 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntry.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntry.cs @@ -25,35 +25,59 @@ namespace Z.EntityFramework.Plus /// An audit entry. public class AuditEntry { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntry() +#if EF5 || EF6 + public void Build(Audit parent, ObjectStateEntry entry) +#else + public void Build(Audit parent, EntityEntry entry) +#endif { - } + if (CreatedBy == null) + { + CreatedBy = parent.CreatedBy; + } + + if (CreatedDate == DateTime.MinValue) + { + CreatedDate = DateTime.Now; + } + + if (Entry == null) + { + Entry = entry; + } + + if (Parent == null) + { + Parent = parent; + } + + if (Properties == null) + { + Properties = new List(); + } - /// Constructor. - /// The audit parent. - /// The object state entry. #if EF5 || EF6 - public AuditEntry(Audit parent, ObjectStateEntry entry) -#elif EFCORE - public AuditEntry(Audit parent, EntityEntry entry) + if (EntitySetName == null) + { + EntitySetName = entry.EntitySet.Name; + } #endif - { - CreatedBy = parent.CreatedBy; - CreatedDate = DateTime.Now; - Entry = entry; - Parent = parent; - Properties = new List(); #if EF5 || EF6 - EntitySetName = entry.EntitySet.Name; - if (!entry.IsRelationship) + + if (EntityTypeName == null) { - EntityTypeName = entry.Entity.GetType().Name; + if (!entry.IsRelationship) + { + EntityTypeName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name; + } } + #elif EFCORE - EntityTypeName = Entry.Entity.GetType().Name; + if (EntityTypeName == null) + { + EntityTypeName = Entry.Entity.GetType().Name; + } #endif } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryFactoryArgs.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryFactoryArgs.cs new file mode 100644 index 00000000..26f5d5db --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryFactoryArgs.cs @@ -0,0 +1,41 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryFactoryArgs + { +#if EF5 || EF6 + public AuditEntryFactoryArgs(Audit audit, ObjectStateEntry objectStateEntry, AuditEntryState auditEntryState) +#elif EFCORE + public AuditEntryFactoryArgs(Audit audit, EntityEntry entityEntry, AuditEntryState auditEntryState) +#endif + { + Audit = audit; + AuditEntryState = auditEntryState; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + + public Audit Audit { get; set; } + public AuditEntryState AuditEntryState { get; set; } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryProperty.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryProperty.cs index 60134f9f..7de7b9dc 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryProperty.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryProperty.cs @@ -13,38 +13,40 @@ namespace Z.EntityFramework.Plus /// An audit entry property. public class AuditEntryProperty { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntryProperty() - { - } - /// Constructor. /// The audit entry parent. /// Name of the property audited. /// The old value audited. /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; + Build(parent, null, propertyName, oldValue, newValue); } - /// Constructor. - /// The audit entry parent. - /// The name of the relation audited. - /// Name of the property audited. - /// The old value audited. - /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; - RelationName = relationName; + InternalPropertyName = propertyName; + + if (!IsValueSet) + { + NewValue = newValue; + OldValue = oldValue; + } + + if (Parent == null) + { + Parent = parent; + } + + if (PropertyName == null) + { + PropertyName = propertyName; + } + + if (RelationName == null) + { + RelationName = relationName; + } } /// Gets or sets the identifier of the audit entry property. @@ -82,6 +84,15 @@ public AuditEntryProperty(AuditEntry parent, string relationName, string propert // EFCORE still have some issue with "NotMapped" attribute public object NewValue; #endif + /// Gets or sets a value indicating whether OldValue and NewValue is set. + /// true if OldValue and NewValue is set, false if not. + [NotMapped] + public bool IsValueSet { get; set; } + + /// Gets or sets the name of the property internally. + /// The name of the property internally. + [NotMapped] + public string InternalPropertyName { get; set; } /// Gets or sets the new value audited formatted. /// The new value audited formatted. diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryPropertyArgs.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryPropertyArgs.cs new file mode 100644 index 00000000..fe8ac325 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Audit/AuditEntryPropertyArgs.cs @@ -0,0 +1,56 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryPropertyArgs + { +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string propertyName, object oldValue, object newValue) + : this(parent, objectStateEntry, null, propertyName, oldValue, newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string propertyName, object oldValue, object newValue) + : this(parent, entityEntry, null, propertyName, oldValue, newValue) +#endif + { + } + +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string relationName, string propertyName, object oldValue, object newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string relationName, string propertyName, object oldValue, object newValue) +#endif + { + AuditEntry = parent; + NewValue = newValue; + OldValue = oldValue; + PropertyName = propertyName; + RelationName = relationName; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + public AuditEntry AuditEntry { get; set; } + public string RelationName { get; set; } + public string PropertyName { get; set; } + public object OldValue { get; set; } + public object NewValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs index 572d5c8f..3f517c9a 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Properties/AssemblyInfo.cs @@ -18,5 +18,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("e4c2af73-caeb-4429-bcb6-0a359484e064")] -[assembly: AssemblyVersion("1.3.12")] -[assembly: AssemblyFileVersion("1.3.12")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0")] +[assembly: AssemblyFileVersion("1.4.0")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj b/src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj index 4d0435eb..6a1a99a3 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj +++ b/src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj @@ -64,7 +64,9 @@ + + diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs index d2220296..09074591 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL +#if FULL || QUERY_FILTER #if EF5 || EF6 #if EF5 diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs index c3c116a7..e67d316b 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE +#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE || QUERY_FILTER #if EF5 || EF6 using System; using System.Data.Entity; diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs index 1b1b7f8e..b211fb7f 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE #if EF5 || EF6 using System.Data.Entity; using System.Linq; diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs index b9b045e5..88f1426b 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED #if EF5 || EF6 using System; using System.Data.Entity.Infrastructure; @@ -41,17 +41,33 @@ internal static ObjectQuery GetObjectQuery(this IQueryable query) if (dbQuery == null) { - throw new Exception(ExceptionMessage.GeneralException); + var internalQueryProperty = query.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + + if (internalQueryProperty == null) + { + throw new Exception(ExceptionMessage.GeneralException); + } + + var internalQuery = internalQueryProperty.GetValue(query, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; + + return objectQuery; } - var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); - var internalQuery = internalQueryProperty.GetValue(dbQuery, null); - var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); - var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + { + var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbQuery, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; - objectQuery = objectQueryContext as ObjectQuery; + return objectQuery; + } - return objectQuery; } } } diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs index f1897025..4991c702 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER #if EF6 using System.Data.Entity; using System.Data.Entity.Core.Objects; diff --git a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs index 615b2f2c..4eb487d0 100644 --- a/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE #if EFCORE using System.Linq; using System.Reflection; diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit.cs index 4828f3cd..20f57234 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit.cs @@ -28,6 +28,23 @@ public Audit() { _configuration = new Lazy(() => AuditManager.DefaultConfiguration.Clone()); Entries = new List(); + + try + { +#if !NETSTANDARD1_3 + CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; +#endif + + if (string.IsNullOrEmpty(CreatedBy)) + { + CreatedBy = "System"; + } + } + catch (Exception) + { + // Oops! it's k, this is the responsability of the user to set the default CreatedBy field + CreatedBy = "System"; + } } /// Gets or sets the entries. diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityAdded.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityAdded.cs index ce2e47b4..9c0eb54e 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityAdded.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityAdded.cs @@ -7,11 +7,14 @@ #if EF5 using System.Data.Objects; +using System.Linq; #elif EF6 using System.Data.Entity.Core.Objects; +using System.Linq; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -29,11 +32,12 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) : + new AuditEntry(); + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityAdded; // CHECK if the key should be resolved in POST Action #if EF5 || EF6 @@ -41,7 +45,7 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) { entry.DelayedKey = objectStateEntry; } - AuditEntityAdded(entry, objectStateEntry.CurrentValues); + AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues); #elif EFCORE // TODO: We must check if the key IsTemporary! We can maybe use flag... //if (!objectStateEntry.IsKeySet) @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #if EF5 || EF6 /// Audit entity added. /// The audit entry. + /// The object state entry. /// The record. /// The prefix. - public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "") + public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord if (valueRecord != null) { // Complex Type - AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) + else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) { - auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value)); + var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ? + auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value); + auditEntry.Properties.Add(auditEntryProperty); } } } @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityDeleted.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityDeleted.cs index 4505955f..845177b1 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityDeleted.cs @@ -6,6 +6,8 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; +using System.Linq; + #if EF5 using System.Data.Objects; @@ -13,6 +15,7 @@ using System.Data.Entity.Core.Objects; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityDeleted; #if EF5 || EF6 - AuditEntityDeleted(entry, objectStateEntry.OriginalValues); + AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues); #elif EFCORE AuditEntityDeleted(entry, objectStateEntry); #endif @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) /// The entry. /// The record. /// The prefix. - public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "") + public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str if (valueRecord != null) { // Complex Type - AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null); + entry.Properties.Add(auditEntryProperty); } } } @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityModified.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityModified.cs index cfc256c0..af5db628 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityModified.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditEntityModified.cs @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = state - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = state; #if EF5 || EF6 AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues); @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) + || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { if (!audit.Configuration.IgnorePropertyUnchanged || objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || !Equals(currentValue, originalValue)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue); + entry.Properties.Add(auditEntryProperty); } } } @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipAdded.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipAdded.cs index 63f78fd3..7351d739 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipAdded.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipAdded.cs @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipAdded; var values = objectStateEntry.CurrentValues; @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipDeleted.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipDeleted.cs index ec5b633a..fa31bedd 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/AuditRelationshipDeleted.cs @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipDeleted; var values = objectStateEntry.OriginalValues; for (var i = 0; i < values.FieldCount; i++) @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr var value = (EntityKey) values.GetValue(i); foreach (var keyValue in value.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/PostSaveChanges.cs b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/PostSaveChanges.cs index 6ae5cf98..99fe1cdb 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/Audit/PostSaveChanges.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/Audit/PostSaveChanges.cs @@ -49,19 +49,29 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } else { foreach (var keyValue in objectStateEntry.EntityKey.EntityKeyValues) { - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Key); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Key); // ENSURE the property is audited if (property != null) @@ -75,7 +85,7 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in objectStateEntry.Metadata.GetKeys()) { var key = objectStateEntry.Property(keyValue.Properties[0].Name); - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Properties[0].Name); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Properties[0].Name); // ENSURE the property is audited if (property != null) diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/AuditConfiguration.cs b/src/Z.EntityFramework.Plus.EF5/Audit/AuditConfiguration.cs index a4271895..5614cb40 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/AuditConfiguration.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/AuditConfiguration.cs @@ -8,8 +8,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -#if EF5 || EF6 + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Core.Objects; #elif EFCORE using Microsoft.EntityFrameworkCore; @@ -38,6 +44,10 @@ public AuditConfiguration() ValueFormatterDictionary = new ConcurrentDictionary>(); } + public Func AuditEntryFactory { get; set; } + + public Func AuditEntryPropertyFactory { get; set; } + /// Gets or sets the automatic audit save pre action. /// The automatic audit save pre action. public Action AutoSavePreAction { get; set; } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntry.cs b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntry.cs index 00301715..babc1094 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntry.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntry.cs @@ -25,35 +25,59 @@ namespace Z.EntityFramework.Plus /// An audit entry. public class AuditEntry { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntry() +#if EF5 || EF6 + public void Build(Audit parent, ObjectStateEntry entry) +#else + public void Build(Audit parent, EntityEntry entry) +#endif { - } + if (CreatedBy == null) + { + CreatedBy = parent.CreatedBy; + } + + if (CreatedDate == DateTime.MinValue) + { + CreatedDate = DateTime.Now; + } + + if (Entry == null) + { + Entry = entry; + } + + if (Parent == null) + { + Parent = parent; + } + + if (Properties == null) + { + Properties = new List(); + } - /// Constructor. - /// The audit parent. - /// The object state entry. #if EF5 || EF6 - public AuditEntry(Audit parent, ObjectStateEntry entry) -#elif EFCORE - public AuditEntry(Audit parent, EntityEntry entry) + if (EntitySetName == null) + { + EntitySetName = entry.EntitySet.Name; + } #endif - { - CreatedBy = parent.CreatedBy; - CreatedDate = DateTime.Now; - Entry = entry; - Parent = parent; - Properties = new List(); #if EF5 || EF6 - EntitySetName = entry.EntitySet.Name; - if (!entry.IsRelationship) + + if (EntityTypeName == null) { - EntityTypeName = entry.Entity.GetType().Name; + if (!entry.IsRelationship) + { + EntityTypeName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name; + } } + #elif EFCORE - EntityTypeName = Entry.Entity.GetType().Name; + if (EntityTypeName == null) + { + EntityTypeName = Entry.Entity.GetType().Name; + } #endif } diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryFactoryArgs.cs b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryFactoryArgs.cs new file mode 100644 index 00000000..26f5d5db --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryFactoryArgs.cs @@ -0,0 +1,41 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryFactoryArgs + { +#if EF5 || EF6 + public AuditEntryFactoryArgs(Audit audit, ObjectStateEntry objectStateEntry, AuditEntryState auditEntryState) +#elif EFCORE + public AuditEntryFactoryArgs(Audit audit, EntityEntry entityEntry, AuditEntryState auditEntryState) +#endif + { + Audit = audit; + AuditEntryState = auditEntryState; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + + public Audit Audit { get; set; } + public AuditEntryState AuditEntryState { get; set; } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryProperty.cs b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryProperty.cs index 60134f9f..7de7b9dc 100644 --- a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryProperty.cs +++ b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryProperty.cs @@ -13,38 +13,40 @@ namespace Z.EntityFramework.Plus /// An audit entry property. public class AuditEntryProperty { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntryProperty() - { - } - /// Constructor. /// The audit entry parent. /// Name of the property audited. /// The old value audited. /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; + Build(parent, null, propertyName, oldValue, newValue); } - /// Constructor. - /// The audit entry parent. - /// The name of the relation audited. - /// Name of the property audited. - /// The old value audited. - /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; - RelationName = relationName; + InternalPropertyName = propertyName; + + if (!IsValueSet) + { + NewValue = newValue; + OldValue = oldValue; + } + + if (Parent == null) + { + Parent = parent; + } + + if (PropertyName == null) + { + PropertyName = propertyName; + } + + if (RelationName == null) + { + RelationName = relationName; + } } /// Gets or sets the identifier of the audit entry property. @@ -82,6 +84,15 @@ public AuditEntryProperty(AuditEntry parent, string relationName, string propert // EFCORE still have some issue with "NotMapped" attribute public object NewValue; #endif + /// Gets or sets a value indicating whether OldValue and NewValue is set. + /// true if OldValue and NewValue is set, false if not. + [NotMapped] + public bool IsValueSet { get; set; } + + /// Gets or sets the name of the property internally. + /// The name of the property internally. + [NotMapped] + public string InternalPropertyName { get; set; } /// Gets or sets the new value audited formatted. /// The new value audited formatted. diff --git a/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryPropertyArgs.cs b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryPropertyArgs.cs new file mode 100644 index 00000000..fe8ac325 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF5/Audit/AuditEntryPropertyArgs.cs @@ -0,0 +1,56 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryPropertyArgs + { +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string propertyName, object oldValue, object newValue) + : this(parent, objectStateEntry, null, propertyName, oldValue, newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string propertyName, object oldValue, object newValue) + : this(parent, entityEntry, null, propertyName, oldValue, newValue) +#endif + { + } + +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string relationName, string propertyName, object oldValue, object newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string relationName, string propertyName, object oldValue, object newValue) +#endif + { + AuditEntry = parent; + NewValue = newValue; + OldValue = oldValue; + PropertyName = propertyName; + RelationName = relationName; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + public AuditEntry AuditEntry { get; set; } + public string RelationName { get; set; } + public string PropertyName { get; set; } + public object OldValue { get; set; } + public object NewValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs index 520f00ea..e0415429 100644 --- a/src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF5/Properties/AssemblyInfo.cs @@ -18,5 +18,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("abcbb878-043c-4957-a334-90e9872e684e")] -[assembly: AssemblyVersion("1.3.12")] -[assembly: AssemblyFileVersion("1.3.12")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0")] +[assembly: AssemblyFileVersion("1.4.0")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF5/Z.EntityFramework.Plus.EF5.csproj b/src/Z.EntityFramework.Plus.EF5/Z.EntityFramework.Plus.EF5.csproj index e39a9332..2e8d9430 100644 --- a/src/Z.EntityFramework.Plus.EF5/Z.EntityFramework.Plus.EF5.csproj +++ b/src/Z.EntityFramework.Plus.EF5/Z.EntityFramework.Plus.EF5.csproj @@ -67,7 +67,9 @@ + + diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs index d2220296..09074591 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL +#if FULL || QUERY_FILTER #if EF5 || EF6 #if EF5 diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs index c3c116a7..e67d316b 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE +#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE || QUERY_FILTER #if EF5 || EF6 using System; using System.Data.Entity; diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs index 1b1b7f8e..b211fb7f 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE #if EF5 || EF6 using System.Data.Entity; using System.Linq; diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs index b9b045e5..88f1426b 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED #if EF5 || EF6 using System; using System.Data.Entity.Infrastructure; @@ -41,17 +41,33 @@ internal static ObjectQuery GetObjectQuery(this IQueryable query) if (dbQuery == null) { - throw new Exception(ExceptionMessage.GeneralException); + var internalQueryProperty = query.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + + if (internalQueryProperty == null) + { + throw new Exception(ExceptionMessage.GeneralException); + } + + var internalQuery = internalQueryProperty.GetValue(query, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; + + return objectQuery; } - var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); - var internalQuery = internalQueryProperty.GetValue(dbQuery, null); - var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); - var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + { + var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbQuery, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; - objectQuery = objectQueryContext as ObjectQuery; + return objectQuery; + } - return objectQuery; } } } diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EF6/ObjectContext/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EF6/ObjectContext/GetDbContext.cs index f1897025..4991c702 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EF6/ObjectContext/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EF6/ObjectContext/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER #if EF6 using System.Data.Entity; using System.Data.Entity.Core.Objects; diff --git a/src/Z.EntityFramework.Plus.EF5/_Internal/EFCore/IQueryable`/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF5/_Internal/EFCore/IQueryable`/GetDbContext.cs index 615b2f2c..4eb487d0 100644 --- a/src/Z.EntityFramework.Plus.EF5/_Internal/EFCore/IQueryable`/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF5/_Internal/EFCore/IQueryable`/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE #if EFCORE using System.Linq; using System.Reflection; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit.cs index 4828f3cd..20f57234 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit.cs @@ -28,6 +28,23 @@ public Audit() { _configuration = new Lazy(() => AuditManager.DefaultConfiguration.Clone()); Entries = new List(); + + try + { +#if !NETSTANDARD1_3 + CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; +#endif + + if (string.IsNullOrEmpty(CreatedBy)) + { + CreatedBy = "System"; + } + } + catch (Exception) + { + // Oops! it's k, this is the responsability of the user to set the default CreatedBy field + CreatedBy = "System"; + } } /// Gets or sets the entries. diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityAdded.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityAdded.cs index ce2e47b4..9c0eb54e 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityAdded.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityAdded.cs @@ -7,11 +7,14 @@ #if EF5 using System.Data.Objects; +using System.Linq; #elif EF6 using System.Data.Entity.Core.Objects; +using System.Linq; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -29,11 +32,12 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) : + new AuditEntry(); + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityAdded; // CHECK if the key should be resolved in POST Action #if EF5 || EF6 @@ -41,7 +45,7 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) { entry.DelayedKey = objectStateEntry; } - AuditEntityAdded(entry, objectStateEntry.CurrentValues); + AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues); #elif EFCORE // TODO: We must check if the key IsTemporary! We can maybe use flag... //if (!objectStateEntry.IsKeySet) @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #if EF5 || EF6 /// Audit entity added. /// The audit entry. + /// The object state entry. /// The record. /// The prefix. - public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "") + public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord if (valueRecord != null) { // Complex Type - AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) + else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) { - auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value)); + var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ? + auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value); + auditEntry.Properties.Add(auditEntryProperty); } } } @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityDeleted.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityDeleted.cs index 4505955f..845177b1 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityDeleted.cs @@ -6,6 +6,8 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; +using System.Linq; + #if EF5 using System.Data.Objects; @@ -13,6 +15,7 @@ using System.Data.Entity.Core.Objects; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityDeleted; #if EF5 || EF6 - AuditEntityDeleted(entry, objectStateEntry.OriginalValues); + AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues); #elif EFCORE AuditEntityDeleted(entry, objectStateEntry); #endif @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) /// The entry. /// The record. /// The prefix. - public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "") + public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str if (valueRecord != null) { // Complex Type - AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null); + entry.Properties.Add(auditEntryProperty); } } } @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityModified.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityModified.cs index cfc256c0..af5db628 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityModified.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditEntityModified.cs @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = state - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = state; #if EF5 || EF6 AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues); @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) + || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { if (!audit.Configuration.IgnorePropertyUnchanged || objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || !Equals(currentValue, originalValue)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue); + entry.Properties.Add(auditEntryProperty); } } } @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipAdded.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipAdded.cs index 63f78fd3..7351d739 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipAdded.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipAdded.cs @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipAdded; var values = objectStateEntry.CurrentValues; @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipDeleted.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipDeleted.cs index ec5b633a..fa31bedd 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/AuditRelationshipDeleted.cs @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipDeleted; var values = objectStateEntry.OriginalValues; for (var i = 0; i < values.FieldCount; i++) @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr var value = (EntityKey) values.GetValue(i); foreach (var keyValue in value.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/PostSaveChanges.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/PostSaveChanges.cs index 6ae5cf98..99fe1cdb 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/PostSaveChanges.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/Audit/PostSaveChanges.cs @@ -49,19 +49,29 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } else { foreach (var keyValue in objectStateEntry.EntityKey.EntityKeyValues) { - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Key); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Key); // ENSURE the property is audited if (property != null) @@ -75,7 +85,7 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in objectStateEntry.Metadata.GetKeys()) { var key = objectStateEntry.Property(keyValue.Properties[0].Name); - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Properties[0].Name); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Properties[0].Name); // ENSURE the property is audited if (property != null) diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditConfiguration.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditConfiguration.cs index a4271895..5614cb40 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditConfiguration.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditConfiguration.cs @@ -8,8 +8,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -#if EF5 || EF6 + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Core.Objects; #elif EFCORE using Microsoft.EntityFrameworkCore; @@ -38,6 +44,10 @@ public AuditConfiguration() ValueFormatterDictionary = new ConcurrentDictionary>(); } + public Func AuditEntryFactory { get; set; } + + public Func AuditEntryPropertyFactory { get; set; } + /// Gets or sets the automatic audit save pre action. /// The automatic audit save pre action. public Action AutoSavePreAction { get; set; } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntry.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntry.cs index 00301715..babc1094 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntry.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntry.cs @@ -25,35 +25,59 @@ namespace Z.EntityFramework.Plus /// An audit entry. public class AuditEntry { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntry() +#if EF5 || EF6 + public void Build(Audit parent, ObjectStateEntry entry) +#else + public void Build(Audit parent, EntityEntry entry) +#endif { - } + if (CreatedBy == null) + { + CreatedBy = parent.CreatedBy; + } + + if (CreatedDate == DateTime.MinValue) + { + CreatedDate = DateTime.Now; + } + + if (Entry == null) + { + Entry = entry; + } + + if (Parent == null) + { + Parent = parent; + } + + if (Properties == null) + { + Properties = new List(); + } - /// Constructor. - /// The audit parent. - /// The object state entry. #if EF5 || EF6 - public AuditEntry(Audit parent, ObjectStateEntry entry) -#elif EFCORE - public AuditEntry(Audit parent, EntityEntry entry) + if (EntitySetName == null) + { + EntitySetName = entry.EntitySet.Name; + } #endif - { - CreatedBy = parent.CreatedBy; - CreatedDate = DateTime.Now; - Entry = entry; - Parent = parent; - Properties = new List(); #if EF5 || EF6 - EntitySetName = entry.EntitySet.Name; - if (!entry.IsRelationship) + + if (EntityTypeName == null) { - EntityTypeName = entry.Entity.GetType().Name; + if (!entry.IsRelationship) + { + EntityTypeName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name; + } } + #elif EFCORE - EntityTypeName = Entry.Entity.GetType().Name; + if (EntityTypeName == null) + { + EntityTypeName = Entry.Entity.GetType().Name; + } #endif } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryFactoryArgs.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryFactoryArgs.cs new file mode 100644 index 00000000..26f5d5db --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryFactoryArgs.cs @@ -0,0 +1,41 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryFactoryArgs + { +#if EF5 || EF6 + public AuditEntryFactoryArgs(Audit audit, ObjectStateEntry objectStateEntry, AuditEntryState auditEntryState) +#elif EFCORE + public AuditEntryFactoryArgs(Audit audit, EntityEntry entityEntry, AuditEntryState auditEntryState) +#endif + { + Audit = audit; + AuditEntryState = auditEntryState; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + + public Audit Audit { get; set; } + public AuditEntryState AuditEntryState { get; set; } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryProperty.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryProperty.cs index 60134f9f..7de7b9dc 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryProperty.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryProperty.cs @@ -13,38 +13,40 @@ namespace Z.EntityFramework.Plus /// An audit entry property. public class AuditEntryProperty { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntryProperty() - { - } - /// Constructor. /// The audit entry parent. /// Name of the property audited. /// The old value audited. /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; + Build(parent, null, propertyName, oldValue, newValue); } - /// Constructor. - /// The audit entry parent. - /// The name of the relation audited. - /// Name of the property audited. - /// The old value audited. - /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; - RelationName = relationName; + InternalPropertyName = propertyName; + + if (!IsValueSet) + { + NewValue = newValue; + OldValue = oldValue; + } + + if (Parent == null) + { + Parent = parent; + } + + if (PropertyName == null) + { + PropertyName = propertyName; + } + + if (RelationName == null) + { + RelationName = relationName; + } } /// Gets or sets the identifier of the audit entry property. @@ -82,6 +84,15 @@ public AuditEntryProperty(AuditEntry parent, string relationName, string propert // EFCORE still have some issue with "NotMapped" attribute public object NewValue; #endif + /// Gets or sets a value indicating whether OldValue and NewValue is set. + /// true if OldValue and NewValue is set, false if not. + [NotMapped] + public bool IsValueSet { get; set; } + + /// Gets or sets the name of the property internally. + /// The name of the property internally. + [NotMapped] + public string InternalPropertyName { get; set; } /// Gets or sets the new value audited formatted. /// The new value audited formatted. diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryPropertyArgs.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryPropertyArgs.cs new file mode 100644 index 00000000..fe8ac325 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Audit/AuditEntryPropertyArgs.cs @@ -0,0 +1,56 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryPropertyArgs + { +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string propertyName, object oldValue, object newValue) + : this(parent, objectStateEntry, null, propertyName, oldValue, newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string propertyName, object oldValue, object newValue) + : this(parent, entityEntry, null, propertyName, oldValue, newValue) +#endif + { + } + +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string relationName, string propertyName, object oldValue, object newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string relationName, string propertyName, object oldValue, object newValue) +#endif + { + AuditEntry = parent; + NewValue = newValue; + OldValue = oldValue; + PropertyName = propertyName; + RelationName = relationName; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + public AuditEntry AuditEntry { get; set; } + public string RelationName { get; set; } + public string PropertyName { get; set; } + public object OldValue { get; set; } + public object NewValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs index d2221233..fb1c0d39 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Properties/AssemblyInfo.cs @@ -18,5 +18,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("ac398eb8-0a31-4d06-a804-84d10b6da96d")] -[assembly: AssemblyVersion("1.3.12")] -[assembly: AssemblyFileVersion("1.3.12")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0")] +[assembly: AssemblyFileVersion("1.4.0")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilter.cs index 8d77aa62..ef47eb5d 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; namespace Z.EntityFramework.Plus @@ -101,4 +102,5 @@ public virtual BaseQueryFilter Clone(QueryFilterContext filterContext) throw new Exception(ExceptionMessage.GeneralException); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilterQueryable.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilterQueryable.cs index 4d1eee71..545cbc3c 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilterQueryable.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/BaseQueryFilterQueryable.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; #if EF5 || EF6 @@ -63,4 +64,5 @@ public virtual void UpdateInternalQuery() throw new Exception(ExceptionMessage.GeneralException); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/DbContext.Filter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/DbContext.Filter.cs index 8672b5a4..74f1e7a8 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/DbContext.Filter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/DbContext.Filter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; @@ -65,4 +66,5 @@ public static BaseQueryFilter Filter(this DbContext context, object key, Func return filter; } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.AsNoFilter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.AsNoFilter.cs index f7fcf22e..52a6b5b3 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.AsNoFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.AsNoFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Linq; #if EF5 || EF6 using System.Data.Entity; @@ -32,4 +33,5 @@ public static IQueryable AsNoFilter(this DbSet query) where T : class return queryFilterQueryable != null ? (IQueryable)queryFilterQueryable.OriginalQuery : query; } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs index 53d78a7a..08a4a987 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/Extensions/IDbSet.Filter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Linq; #if EF5 || EF6 using System.Data.Entity; @@ -40,4 +41,5 @@ public static IQueryable Filter(this DbSet query, params object[] keys) return filterContext.ApplyFilter(query, keys); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilter.cs index 6a9f09b0..b3a923dd 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; @@ -57,4 +58,5 @@ public override BaseQueryFilter Clone(QueryFilterContext filterContext) return new QueryFilter(filterContext, Filter); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterContext.cs index d9255538..bbda72a2 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterContext.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterContext.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; using System.Linq; @@ -212,7 +213,7 @@ public void LoadGenericContextInfo(DbContext context) FilterSetByType.AddOrAppend(@interface, filterDbSet); } -#if NETCORE50 +#if NETSTANDARD1_3 baseType = baseType.GetTypeInfo().BaseType; #else baseType = baseType.BaseType; @@ -222,4 +223,5 @@ public void LoadGenericContextInfo(DbContext context) } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterManager.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterManager.cs index b93a930c..579c88ce 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterManager.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterManager.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; using System.Linq; @@ -174,4 +175,5 @@ public static void InitilizeGlobalFilter(DbContext context) } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterQueryable.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterQueryable.cs index 8d182e3b..f3bbc746 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterQueryable.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterQueryable.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Collections.Generic; using System.Linq; #if EF5 @@ -59,4 +60,5 @@ public override void UpdateInternalQuery() #endif } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterSet.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterSet.cs index f04d8c53..a48f8e0c 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterSet.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilter/QueryFilterSet.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; using System.Linq.Expressions; @@ -191,4 +192,5 @@ public void UpdateInternalQuery(DbContext context, object query) } #endif } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs new file mode 100644 index 00000000..23106a44 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs @@ -0,0 +1,154 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus +{ + /// A base class for query filter. + public abstract class BaseQueryFilterInterceptor + { + /// The unique key. + public readonly string UniqueKey = Guid.NewGuid().ToString(); + + /// List of is filter enabled. + public List> IsFilterEnabledList = new List>(); + + /// Gets or sets the context that owns this item. + /// The owner context. + public QueryFilterContextInterceptor OwnerContext { get; set; } + + /// Gets or sets a value indicating whether the filter is enabled by default. + /// true if the filter is enabled by default, false if not. + public bool IsDefaultEnabled { get; set; } + + /// Gets or sets the type of the element. + /// The type of the element. + public Type ElementType { get; set; } + + /// Gets database expression. + /// The context. + /// The type. + /// The database expression. + public abstract DbExpression GetDbExpression(DbContext context, Type type); + + /// Disables this filter. + public void Disable() + { + Disable(null); + } + + /// Disables this filter on the speficied type. + /// Type of the element to disable the filter on. + public void Disable() + { + Disable(typeof(TType)); + } + + /// Disable this filter on the specified types. + /// A variable-length parameters list containing types to disable the filter on. + public void Disable(params Type[] types) + { + if (types == null || types.Length == 0) + { + if (IsFilterEnabledList.Count == 0) + { + IsDefaultEnabled = false; + } + else + { + IsFilterEnabledList.Add(type1 => false); + } + } + else + { + foreach (var type in types) + { + IsFilterEnabledList.Add(type1 => type.IsAssignableFrom(type1) ? false : (bool?)null); + } + } + + if (OwnerContext != null) + { + OwnerContext.ClearCache(); + } + else + { + QueryFilterManager.ClearAllCache(); + } + } + + /// Enables this filter. + public void Enable() + { + Enable(null); + } + + /// Enables this filter on the speficied type. + /// Type of the element to enable the filter on. + public void Enable() + { + Enable(typeof(TType)); + } + + /// Enables this filter on the specified types. + /// A variable-length parameters list containing types to enable the filter on. + public void Enable(params Type[] types) + { + if (types == null || types.Length == 0) + { + if (IsFilterEnabledList.Count == 0) + { + IsDefaultEnabled = true; + } + else + { + IsFilterEnabledList.Add(type1 => true); + } + } + else + { + foreach (var type in types) + { + IsFilterEnabledList.Add(type1 => type.IsAssignableFrom(type1) ? true : (bool?)null); + } + } + + if (OwnerContext != null) + { + OwnerContext.ClearCache(); + } + else + { + QueryFilterManager.ClearAllCache(); + } + } + + /// Queries if a type is enabled. + /// The type. + /// true if a type is enabled, false if not. + public bool IsTypeEnabled(Type type) + { + var isEnabled = IsDefaultEnabled; + + foreach (var isFiltedEnabled in IsFilterEnabledList) + { + var isEnabledFactory = isFiltedEnabled(type); + + if (isEnabledFactory.HasValue) + { + isEnabled = isEnabledFactory.Value; + } + } + + return isEnabled; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/DbContext.Filter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/DbContext.Filter.cs new file mode 100644 index 00000000..2da236d1 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/DbContext.Filter.cs @@ -0,0 +1,61 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Data.Entity; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// Gets the filter associated with the specified key from the context. + /// The context filtered. + /// The filter key associated to the filter. + /// The filter associated with the specified key from the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, object key) + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(context); + return filterContext.GetFilter(key); + } + + /// + /// Creates and return a filter added for the context. + /// + /// The type of elements of the query. + /// The context to filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + return context.Filter(Guid.NewGuid(), queryFilter, isEnabled); + } + + /// + /// Creates and return a filter associated with the specified key added for the context. + /// + /// The type of elements of the query. + /// The context filtered. + /// The filter key associated to the filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, object key, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(context); + var filter = filterContext.AddFilter(key, queryFilter); + + if (isEnabled) + { + filter.Enable(); + } + + return filter; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs new file mode 100644 index 00000000..a5eef715 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs @@ -0,0 +1,24 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Data.Entity; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// Return the orginal query before the context was filtered. + /// The type of elements of the query. + /// The filtered query from which the original query should be retrieved. + /// The orginal query before the context was filtered. + public static IQueryable AsNoFilter(this IDbSet query) where T : class + { + return QueryFilterManager.HookFilter(query, QueryFilterManager.DisableAllFilter); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs new file mode 100644 index 00000000..98718ce2 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs @@ -0,0 +1,50 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Data.Entity; +using System.Linq; +using System.Text; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// + /// Filter the query using context filters associated with specified keys. + /// + /// The type of elements of the query. + /// The query to filter using context filters associated with specified keys. + /// + /// A variable-length parameters list containing keys associated to context filters to use to filter the + /// query. + /// + /// The query filtered using context filters associated with specified keys. + public static IQueryable Filter(this IDbSet query, params object[] keys) where T : class + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(query.GetDbContext()); + var filterHook = QueryFilterManager.EnableFilterById; + + var sb = new StringBuilder(); + + if (keys != null) + { + foreach (var key in keys) + { + var filter = filterContext.GetFilter(key); + if (filter == null) + { + continue; + } + sb.Append(filter.UniqueKey); + sb.Append(";"); + } + } + + return QueryFilterManager.HookFilter(query.AsNoFilter(), filterHook + sb); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptor.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptor.cs new file mode 100644 index 00000000..07748b42 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptor.cs @@ -0,0 +1,62 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + /// A class for query filter. + /// The type of the filter element. + public class QueryFilterInterceptor : BaseQueryFilterInterceptor where T : class + { + /// Constructor. + /// The filter. + public QueryFilterInterceptor(Func, IQueryable> filter) + { + ElementType = typeof(T); + Filter = filter; + } + + /// Gets or sets the filter. + /// The filter. + public Func, IQueryable> Filter { get; set; } + + /// Gets database expression. + /// The context. + /// The type. + /// The database expression. + public override DbExpression GetDbExpression(DbContext context, Type type) + { + var contextFullName = context.GetType().AssemblyQualifiedName ?? context.GetType().FullName; + var typeFullName = type.AssemblyQualifiedName ?? type.FullName; + var hookId = QueryFilterManager.PrefixHook + contextFullName + ";" + typeFullName + ";" + UniqueKey; + + if (!QueryFilterManager.DbExpressionByHook.ContainsKey(hookId)) + { + // CREATE set + var setMethod = typeof(DbContext).GetMethod("Set", new Type[0]).MakeGenericMethod(type); + var dbSet = (IQueryable)setMethod.Invoke(context, null); + + // APPLY filter + dbSet = Filter(dbSet); + + // APPLY hook + dbSet = QueryFilterManager.HookFilter(dbSet, hookId); + + // Hook the filter + var objectQuery = dbSet.GetObjectQuery(); + objectQuery.ToTraceString(); + } + + // TODO: WeakTable ? + return QueryFilterManager.DbExpressionByHook[hookId]; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorApply.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorApply.cs new file mode 100644 index 00000000..b8d9ac91 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorApply.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +namespace Z.EntityFramework.Plus +{ + public class QueryFilterInterceptorApply + { + /// List of apply filters. + public List> ApplyFilterList = new List>(); + + /// The global filters. + public QueryFilterContextInterceptor GlobalFilters; + + /// The instance filters. + public QueryFilterContextInterceptor InstanceFilters; + + /// Query if 'filter' is enabled. + /// Specifies the filter. + /// true if enabled, false if not. + public bool? IsEnabled(BaseQueryFilterInterceptor filter) + { + bool? isEnabled = null; + + foreach (var applyFilter in ApplyFilterList) + { + var shouldEnable = applyFilter(filter); + + if (shouldEnable.HasValue) + { + isEnabled = shouldEnable.Value; + } + } + + return isEnabled; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorContext.cs new file mode 100644 index 00000000..b6a3d6d2 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorContext.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Metadata.Edm; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Reflection; + +namespace Z.EntityFramework.Plus +{ + public class QueryFilterContextInterceptor + { + /// true if clear cache required. + public bool ClearCacheRequired; + + /// The filter by entity set base. + public Dictionary> FilterByEntitySetBase = new Dictionary>(); + + /// Gets or sets the filters. + /// The filters. + public ConcurrentDictionary FilterByKey = new ConcurrentDictionary(); + + /// Type of the filter by. + public ConcurrentDictionary> FilterByType = new ConcurrentDictionary>(); + + /// Gets or sets the filters. + /// The filters. + public ConcurrentDictionary GlobalFilterByKey = new ConcurrentDictionary(); + + /// Type of the filter by. + public ConcurrentDictionary> GlobalFilterByType = new ConcurrentDictionary>(); + + /// Set the type by database belongs to. + public Dictionary> TypeByDbSet = new Dictionary>(); + + /// The type by entity set base. + public Dictionary TypeByEntitySetBase = new Dictionary(); + + public QueryFilterContextInterceptor(DbContext context) + { + Context = context; + Initialize(context); + } + + /// Gets or sets the context associated with the filter context. + /// The context associated with the filter context. + public DbContext Context { get; set; } + + /// Gets applicable filter. + /// Name of the database set. + /// The applicable filter. + public List GetApplicableFilter(string dbSetName) + { + var list = new List(); + + var types = TypeByDbSet[dbSetName]; + + foreach (var type in types) + { + List filterList; + if (FilterByType.TryGetValue(type, out filterList)) + { + list.AddRange(filterList); + } + } + + return list; + } + + /// Gets global applicable filter. + /// Name of the database set. + /// The global applicable filter. + public List GetGlobalApplicableFilter(string dbSetName) + { + var list = new List(); + + var types = TypeByDbSet[dbSetName]; + + foreach (var type in types) + { + List filterList; + if (GlobalFilterByType.TryGetValue(type, out filterList)) + { + list.AddRange(filterList); + } + } + + return list; + } + + /// Adds a query filter to the filter context associated with the specified key. + /// The type of elements of the query. + /// The filter key. + /// The filter. + /// The query filter added to the filter context associated with the specified ke . + public BaseQueryFilterInterceptor AddFilter(object key, Func, IQueryable> filter) where T : class + { + var queryFilter = new QueryFilterInterceptor(filter); + queryFilter.OwnerContext = this; + + // FilterByKey + { + FilterByKey.AddOrUpdate(key, queryFilter, (o, interceptorFilter) => queryFilter); + } + + // FilterByType + { + if (!FilterByType.ContainsKey(typeof (T))) + { + FilterByType.TryAdd(typeof (T), new List()); + } + + FilterByType[typeof (T)].Add(queryFilter); + } + + ClearCache(); + return queryFilter; + } + + /// Clears the cache. + public void ClearCache() + { + if (ClearCacheRequired) + { + QueryFilterManager.ClearQueryCache(Context); + ClearCacheRequired = false; + } + } + + /// Gets the filter associated to the specified key. + /// The filter key. + /// The filter associated to the specified key. + public BaseQueryFilterInterceptor GetFilter(object key) + { + BaseQueryFilterInterceptor filter; + if (!FilterByKey.TryGetValue(key, out filter)) + { + GlobalFilterByKey.TryGetValue(key, out filter); + } + return filter; + } + + /// Initializes this object. + /// The context. + private void Initialize(DbContext context) + { + // GET DbSet<> properties + var setProperties = context.GetDbSetProperties(); + + foreach (var setProperty in setProperties) + { + // GET DbSet<> + var dbSet = (IQueryable) setProperty.GetValue(context, null); + + // DbSet<>.InternalQuery + var internalQueryProperty = typeof (DbQuery<>).MakeGenericType(dbSet.ElementType).GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbSet, null); + + // DbSet<>.InternalQuery.EntitySet + var entitySetProperty = internalQuery.GetType().GetProperty("EntitySet", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var entitySet = (EntitySet) entitySetProperty.GetValue(internalQuery, null); + + var elementType = dbSet.ElementType; + var entityTypebase = entitySet.ElementType.FullName; + + // TypeByEntitySetBase + { + if (!TypeByEntitySetBase.ContainsKey(entityTypebase)) + { + TypeByEntitySetBase.Add(entityTypebase, elementType); + } + } + + // TypeByDbSet + { + var baseType = elementType; + + var types = new List(); + while (baseType != null && baseType != typeof (object)) + { + types.Add(baseType); + + // LINK interface + var interfaces = baseType.GetInterfaces(); + foreach (var @interface in interfaces) + { + types.Add(@interface); + } + + baseType = baseType.BaseType; + } + + // ENSURE all discting + types = types.Distinct().ToList(); + + if (!TypeByDbSet.ContainsKey(entityTypebase)) + { + TypeByDbSet.Add(entityTypebase, types); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs new file mode 100644 index 00000000..ce17810c --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs @@ -0,0 +1,89 @@ +using System; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Infrastructure.Interception; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + /// A query interceptor filter database command tree. + public class QueryFilterInterceptorDbCommandTree : IDbCommandTreeInterceptor + { + /// + /// This method is called after a new + /// has been created. + /// The tree that is used after interception can be changed by setting + /// + /// while intercepting. + /// + /// Contextual information associated with the call. + public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) + { + var dbQueryCommandTree = interceptionContext.Result as DbQueryCommandTree; + if (dbQueryCommandTree != null && interceptionContext.DbContexts.Count() == 1) + { + var context = interceptionContext.DbContexts.First(); + + // Visit first to find filter ID && hook + var visitorFilter = new QueryFilterInterceptorDbFilterExpression(); + var queryFiltered = dbQueryCommandTree.Query.Accept(visitorFilter); + + if (!string.IsNullOrEmpty(visitorFilter.HookID)) + { + if (!QueryFilterManager.DbExpressionByHook.ContainsKey(visitorFilter.HookID)) + { + QueryFilterManager.DbExpressionByHook.TryAdd(visitorFilter.HookID, queryFiltered); + } + } + else + { + var filterByContext = QueryFilterManager.AddOrGetFilterContext(context); + filterByContext.ClearCacheRequired = true; + + var filterQuery = new QueryFilterInterceptorApply + { + InstanceFilters = filterByContext + }; + + if (visitorFilter.FilterID != null && visitorFilter.FilterID.Count > 0) + { + foreach (var filter in visitorFilter.FilterID) + { + if (filter == QueryFilterManager.DisableAllFilter) + { + // Disable all filter in the context! + filterQuery.ApplyFilterList.Add(interceptorFilter => false); + } + else if (filter.StartsWith(QueryFilterManager.EnableFilterById, StringComparison.InvariantCulture)) + { + // Enable all specific filter + var filters = filter.Substring(QueryFilterManager.EnableFilterById.Length).Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); + + if (filters.Length == 0) + { + filterQuery.ApplyFilterList.Add(interceptorFilter => false); + } + foreach (var applyFilter in filters) + { + filterQuery.ApplyFilterList.Add(interceptorFilter => interceptorFilter.UniqueKey.ToString() == applyFilter ? true : (bool?)null); + } + } + } + } + + // VISIT filter + var visitor = new QueryFilterInterceptorDbScanExpression + { + Context = context, + InstanceFilterContext = filterByContext, + FilterQuery = filterQuery + }; + + var newQuery = queryFiltered.Accept(visitor); + + // CREATE a new Query + interceptionContext.Result = new DbQueryCommandTree(dbQueryCommandTree.MetadataWorkspace, dbQueryCommandTree.DataSpace, newQuery, true); + } + } + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs new file mode 100644 index 00000000..d5cd00f7 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus +{ + /// A query interceptor filter database filter expression. + public class QueryFilterInterceptorDbFilterExpression : DefaultExpressionVisitor + { + /// Identifier for the filter. + public List FilterID; + + /// Identifier for the hook. + public string HookID; + + /// + /// Implements the visitor pattern for a predicate applied to filter an input set. + /// + /// The filter expression. + /// The implemented visitor. + public override DbExpression Visit(DbFilterExpression expression) + { + var predicate = expression.Predicate; + var comparison = predicate as DbComparisonExpression; + + if (comparison != null) + { + var leftConstant = comparison.Left as DbConstantExpression; + + if (leftConstant != null) + { + { + var valueString = leftConstant.Value as string; + + if (valueString != null) + { + if (valueString.StartsWith(QueryFilterManager.PrefixFilter, StringComparison.InvariantCulture)) + { + if (FilterID == null) + { + FilterID = new List(); + } + FilterID.Add(valueString); + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + if (valueString.StartsWith(QueryFilterManager.PrefixHook, StringComparison.InvariantCulture)) + { + HookID = valueString; + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + } + } + } + + var rightConstant = comparison.Right as DbConstantExpression; + + if (rightConstant != null) + { + { + var valueString = rightConstant.Value as string; + + if (valueString != null) + { + if (valueString.StartsWith(QueryFilterManager.PrefixFilter, StringComparison.InvariantCulture)) + { + if (FilterID == null) + { + FilterID = new List(); + } + FilterID.Add(valueString); + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + if (valueString.StartsWith(QueryFilterManager.PrefixHook, StringComparison.InvariantCulture)) + { + HookID = valueString; + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + } + } + } + } + + var baseExpression = base.Visit(expression); + return baseExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs new file mode 100644 index 00000000..950a8061 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs @@ -0,0 +1,44 @@ +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus.QueryInterceptorFilter +{ + /// A database project to input expression visitor. + public class QueryFilterInterceptorDbProjectExpression : DefaultExpressionVisitor + { + /// The database scan expression. + public DbExpression DbScanExpression; + + /// + /// Implements the visitor pattern for the projection of a given input set over the specified + /// expression. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbProjectExpression expression) + { + var baseExpression = base.Visit(expression); + var baseDbProject = baseExpression as DbProjectExpression; + + if (baseDbProject != null) + { + return baseDbProject.Input.Expression; + } + + + // This situation may happen when another user-defined interceptor is used + // The library may not be compatible in this situation + return baseExpression; + } + + /// + /// Implements the visitor pattern for a scan over an entity set or relationship set, as + /// indicated by the Target property. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbScanExpression expression) + { + return DbScanExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs new file mode 100644 index 00000000..d33d25cf --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs @@ -0,0 +1,131 @@ +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Core.Metadata.Edm; +using Z.EntityFramework.Plus.QueryInterceptorFilter; + +namespace Z.EntityFramework.Plus +{ + /// A database scan expression visitor. + public class QueryFilterInterceptorDbScanExpression : DefaultExpressionVisitor + { + /// The context. + public DbContext Context; + + /// The filtered expression. + public DbExpression FilteredExpression; + + /// The filter query. + public QueryFilterInterceptorApply FilterQuery; + + /// Context for the instance filter. + public QueryFilterContextInterceptor InstanceFilterContext; + + /// + /// Implements the visitor pattern for a scan over an entity set or relationship set, as + /// indicated by the Target property. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbScanExpression expression) + { + var baseExpression = base.Visit(expression); + var baseType = expression.Target.ElementType; + var fullName = baseType.FullName; + + baseExpression = ApplyFilter(baseExpression, fullName); + + return baseExpression; + } + + /// Implements the visitor pattern for retrieving an instance property. + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbPropertyExpression expression) + { + var baseExpression = base.Visit(expression); + var baseExpressionProperty = baseExpression as DbPropertyExpression; + + if (baseExpressionProperty == null) + { + return baseExpression; + } + + var navProp = baseExpressionProperty.Property as NavigationProperty; + if (navProp != null && baseExpression.ResultType.ToString().Contains("Transient.collection[")) + { + var targetEntityType = navProp.ToEndMember.GetEntityType(); + var fullName = targetEntityType.FullName; + + baseExpression = ApplyFilter(baseExpression, fullName); + } + + return baseExpression; + } + + /// Applies the filter. + /// The base expression. + /// Name of the full. + /// A DbExpression. + public DbExpression ApplyFilter(DbExpression baseExpression, string fullName) + { + if (InstanceFilterContext.TypeByEntitySetBase.ContainsKey(fullName)) + { + var filters = InstanceFilterContext.GetGlobalApplicableFilter(fullName); + var type = InstanceFilterContext.TypeByEntitySetBase[fullName]; + + if (filters.Count > 0) + { + foreach (var filter in filters) + { + var filterQueryEnabled = FilterQuery.IsEnabled(filter); + if ((filterQueryEnabled.HasValue && !filterQueryEnabled.Value) + || (!filterQueryEnabled.HasValue && !filter.IsTypeEnabled(type))) + { + continue; + } + + var expression2 = filter.GetDbExpression(Context, type); + + if (expression2 != null) + { + var visitor = new QueryFilterInterceptorDbProjectExpression(); + visitor.DbScanExpression = baseExpression; + var filetered = expression2.Accept(visitor); + baseExpression = filetered; + } + } + } + } + if (InstanceFilterContext.TypeByEntitySetBase.ContainsKey(fullName)) + { + var filters = InstanceFilterContext.GetApplicableFilter(fullName); + var type = InstanceFilterContext.TypeByEntitySetBase[fullName]; + + if (filters.Count > 0) + { + foreach (var filter in filters) + { + var filterQueryEnabled = FilterQuery.IsEnabled(filter); + if ((filterQueryEnabled.HasValue && !filterQueryEnabled.Value) + || (!filterQueryEnabled.HasValue && !filter.IsTypeEnabled(type))) + { + continue; + } + + var expression2 = filter.GetDbExpression(Context, type); + + if (expression2 != null) + { + var visitor = new QueryFilterInterceptorDbProjectExpression(); + visitor.DbScanExpression = baseExpression; + var filetered = expression2.Accept(visitor); + baseExpression = filetered; + } + } + } + } + + return baseExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorManager.cs b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorManager.cs new file mode 100644 index 00000000..f0427fd1 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6.NET40/QueryFilterInterceptor/QueryFilterInterceptorManager.cs @@ -0,0 +1,221 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Infrastructure.Interception; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace Z.EntityFramework.Plus +{ + /// A class to manage query interceptor filter options. + public static class QueryFilterManager + { + /// A filter specifying the prefix. + internal static string PrefixFilter = "Z.EntityFramework.Plus.QueryFilterInterceptor.Filer;"; + + /// The prefix hook. + internal static string PrefixHook = "Z.EntityFramework.Plus.QueryFilterInterceptor.Hook;"; + + /// A filter specifying the disable all. + internal static string DisableAllFilter = PrefixFilter + "DisableAll;"; + + /// Identifier for the enable filter by. + internal static string EnableFilterById = PrefixFilter + "EnableById;"; + + /// Static constructor. + static QueryFilterManager() + { + DbInterception.Add(new QueryFilterInterceptorDbCommandTree()); + + CacheWeakFilterContext = new ConditionalWeakTable(); + GlobalFiltersByKey = new ConcurrentDictionary(); + GlobalFilterByType = new ConcurrentDictionary>(); + DbExpressionByHook = new ConcurrentDictionary(); + } + + /// Gets the database expression by hook. + /// The database expression by hook. + public static ConcurrentDictionary DbExpressionByHook { get; } + + /// Gets the global filters. + /// The global filters. + public static ConcurrentDictionary GlobalFiltersByKey { get; } + + /// Gets or sets the type of the global filter by. + /// The type of the global filter by. + public static ConcurrentDictionary> GlobalFilterByType { get; set; } + + /// Gets or sets the weak table containing filter context for a specified context. + /// The weak table containing filter context for a specified context. + public static ConditionalWeakTable CacheWeakFilterContext { get; set; } + + /// Gets the filter associated with the specified key from the context. + /// The filter key associated to the filter. + /// The filter associated with the specified key from the context. + public static BaseQueryFilterInterceptor Filter(object key) + { + BaseQueryFilterInterceptor filter; + GlobalFiltersByKey.TryGetValue(key, out filter); + + return filter; + } + + /// + /// Creates and return a filter added for the context. + /// + /// The type of elements of the query. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + return Filter(Guid.NewGuid(), queryFilter, isEnabled); + } + + /// + /// Creates and return a filter associated with the specified key added for the context. + /// + /// The type of elements of the query. + /// The filter key associated to the filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(object key, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + BaseQueryFilterInterceptor filter; + + // FilterByKey + { + if (!GlobalFiltersByKey.TryGetValue(key, out filter)) + { + filter = new QueryFilterInterceptor(queryFilter) {IsDefaultEnabled = isEnabled}; + GlobalFiltersByKey.AddOrUpdate(key, filter, (o, interceptorFilter) => filter); + } + } + + // FilterByType + { + if (!GlobalFilterByType.ContainsKey(typeof (T))) + { + GlobalFilterByType.AddOrUpdate(typeof (T), new List(), (type, list) => list); + } + + GlobalFilterByType[typeof (T)].Add(filter); + } + + ClearAllCache(); + + return filter; + } + + /// Adds or get the filter context associated with the context. + /// The context associated with the filter context. + /// The filter context associated with the context. + public static QueryFilterContextInterceptor AddOrGetFilterContext(DbContext context) + { + QueryFilterContextInterceptor filterContext; + + if (!CacheWeakFilterContext.TryGetValue(context, out filterContext)) + { + filterContext = new QueryFilterContextInterceptor(context) + { + GlobalFilterByKey = GlobalFiltersByKey, + GlobalFilterByType = GlobalFilterByType + }; + CacheWeakFilterContext.Add(context, filterContext); + } + + return filterContext; + } + + /// Hook filter. + /// Generic type parameter. + /// The query. + /// The value. + /// An IQueryable<T> + public static IQueryable HookFilter(IQueryable query, string value) + { + // CREATE hook + var parameter = Expression.Parameter(typeof (T)); + var left = Expression.Constant(value); + var right = Expression.Constant(value); + var predicate = Expression.Equal(left, right); + var lambda = Expression.Lambda>(predicate, parameter); + + // APPLY hook + query = query.Where(lambda); + + return query; + } + + /// Initilize global filter in the context. + /// The context to initialize global filter on. + public static void InitilizeGlobalFilter(DbContext context) + { + // NOT anymore required! Remove this on v2? + } + + /// Clears the query cache described by context. + /// The context to initialize global filter on. + public static void ClearQueryCache(DbContext context) + { + try + { + var objectContext = context.GetObjectContext(); + var metaWorkspace = objectContext.MetadataWorkspace; + + var getQueryCacheManagerMethod = metaWorkspace.GetType().GetMethod("GetQueryCacheManager", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var getQueryCacheManager = getQueryCacheManagerMethod.Invoke(metaWorkspace, null); + + var clearMethod = getQueryCacheManager.GetType().GetMethod("Clear", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + clearMethod.Invoke(getQueryCacheManager, null); + + // GET DbSet<> properties + var setProperties = context.GetDbSetProperties(); + + foreach (var setProperty in setProperties) + { + var dbSet = (IQueryable) setProperty.GetValue(context, null); + + // Disable Set Caching + { + var objectQuery = dbSet.GetObjectQuery(); + + var stateField = objectQuery.GetType().BaseType.GetField("_state", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var state = stateField.GetValue(objectQuery); + + var cachedPlanField = state.GetType().GetField("_cachedPlan", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + cachedPlanField.SetValue(state, null); + } + } + } + catch (Exception) + { + // TODO: Find a better way to handle this! + } + } + + /// Clears all cache. + public static void ClearAllCache() + { + var propertyValues = CacheWeakFilterContext.GetType().GetProperty("Values", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var values = (List) propertyValues.GetValue(CacheWeakFilterContext, null); + + foreach (var value in values) + { + value.ClearCache(); + } + } + + /// Clears the global filter. + public static void ClearGlobalFilter() + { + GlobalFiltersByKey.Clear(); + GlobalFilterByType.Clear(); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/Z.EntityFramework.Plus.EF6.NET40.csproj b/src/Z.EntityFramework.Plus.EF6.NET40/Z.EntityFramework.Plus.EF6.NET40.csproj index abe0f74f..9810b59d 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/Z.EntityFramework.Plus.EF6.NET40.csproj +++ b/src/Z.EntityFramework.Plus.EF6.NET40/Z.EntityFramework.Plus.EF6.NET40.csproj @@ -63,7 +63,9 @@ + + @@ -118,6 +120,18 @@ + + + + + + + + + + + + diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs index d2220296..09074591 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL +#if FULL || QUERY_FILTER #if EF5 || EF6 #if EF5 diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs index c3c116a7..e67d316b 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE +#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE || QUERY_FILTER #if EF5 || EF6 using System; using System.Data.Entity; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs index 1b1b7f8e..b211fb7f 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE #if EF5 || EF6 using System.Data.Entity; using System.Linq; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs index b9b045e5..88f1426b 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED #if EF5 || EF6 using System; using System.Data.Entity.Infrastructure; @@ -41,17 +41,33 @@ internal static ObjectQuery GetObjectQuery(this IQueryable query) if (dbQuery == null) { - throw new Exception(ExceptionMessage.GeneralException); + var internalQueryProperty = query.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + + if (internalQueryProperty == null) + { + throw new Exception(ExceptionMessage.GeneralException); + } + + var internalQuery = internalQueryProperty.GetValue(query, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; + + return objectQuery; } - var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); - var internalQuery = internalQueryProperty.GetValue(dbQuery, null); - var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); - var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + { + var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbQuery, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; - objectQuery = objectQueryContext as ObjectQuery; + return objectQuery; + } - return objectQuery; } } } diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs index f1897025..4991c702 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EF6/ObjectContext/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER #if EF6 using System.Data.Entity; using System.Data.Entity.Core.Objects; diff --git a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs index 615b2f2c..4eb487d0 100644 --- a/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EF6.NET40/_Internal/EFCore/IQueryable`/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE #if EFCORE using System.Linq; using System.Reflection; diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit.cs index 4828f3cd..20f57234 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit.cs @@ -28,6 +28,23 @@ public Audit() { _configuration = new Lazy(() => AuditManager.DefaultConfiguration.Clone()); Entries = new List(); + + try + { +#if !NETSTANDARD1_3 + CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; +#endif + + if (string.IsNullOrEmpty(CreatedBy)) + { + CreatedBy = "System"; + } + } + catch (Exception) + { + // Oops! it's k, this is the responsability of the user to set the default CreatedBy field + CreatedBy = "System"; + } } /// Gets or sets the entries. diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityAdded.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityAdded.cs index ce2e47b4..9c0eb54e 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityAdded.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityAdded.cs @@ -7,11 +7,14 @@ #if EF5 using System.Data.Objects; +using System.Linq; #elif EF6 using System.Data.Entity.Core.Objects; +using System.Linq; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -29,11 +32,12 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) : + new AuditEntry(); + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityAdded; // CHECK if the key should be resolved in POST Action #if EF5 || EF6 @@ -41,7 +45,7 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) { entry.DelayedKey = objectStateEntry; } - AuditEntityAdded(entry, objectStateEntry.CurrentValues); + AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues); #elif EFCORE // TODO: We must check if the key IsTemporary! We can maybe use flag... //if (!objectStateEntry.IsKeySet) @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #if EF5 || EF6 /// Audit entity added. /// The audit entry. + /// The object state entry. /// The record. /// The prefix. - public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "") + public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord if (valueRecord != null) { // Complex Type - AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) + else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) { - auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value)); + var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ? + auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value); + auditEntry.Properties.Add(auditEntryProperty); } } } @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityDeleted.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityDeleted.cs index 4505955f..845177b1 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityDeleted.cs @@ -6,6 +6,8 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; +using System.Linq; + #if EF5 using System.Data.Objects; @@ -13,6 +15,7 @@ using System.Data.Entity.Core.Objects; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityDeleted; #if EF5 || EF6 - AuditEntityDeleted(entry, objectStateEntry.OriginalValues); + AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues); #elif EFCORE AuditEntityDeleted(entry, objectStateEntry); #endif @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) /// The entry. /// The record. /// The prefix. - public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "") + public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str if (valueRecord != null) { // Complex Type - AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null); + entry.Properties.Add(auditEntryProperty); } } } @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityModified.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityModified.cs index cfc256c0..af5db628 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityModified.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditEntityModified.cs @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = state - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = state; #if EF5 || EF6 AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues); @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) + || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { if (!audit.Configuration.IgnorePropertyUnchanged || objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || !Equals(currentValue, originalValue)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue); + entry.Properties.Add(auditEntryProperty); } } } @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipAdded.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipAdded.cs index 63f78fd3..7351d739 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipAdded.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipAdded.cs @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipAdded; var values = objectStateEntry.CurrentValues; @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipDeleted.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipDeleted.cs index ec5b633a..fa31bedd 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipDeleted.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/AuditRelationshipDeleted.cs @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipDeleted; var values = objectStateEntry.OriginalValues; for (var i = 0; i < values.FieldCount; i++) @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr var value = (EntityKey) values.GetValue(i); foreach (var keyValue in value.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/PostSaveChanges.cs b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/PostSaveChanges.cs index 6ae5cf98..99fe1cdb 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/Audit/PostSaveChanges.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/Audit/PostSaveChanges.cs @@ -49,19 +49,29 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } else { foreach (var keyValue in objectStateEntry.EntityKey.EntityKeyValues) { - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Key); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Key); // ENSURE the property is audited if (property != null) @@ -75,7 +85,7 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in objectStateEntry.Metadata.GetKeys()) { var key = objectStateEntry.Property(keyValue.Properties[0].Name); - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Properties[0].Name); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Properties[0].Name); // ENSURE the property is audited if (property != null) diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/AuditConfiguration.cs b/src/Z.EntityFramework.Plus.EF6/Audit/AuditConfiguration.cs index a4271895..5614cb40 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/AuditConfiguration.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/AuditConfiguration.cs @@ -8,8 +8,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -#if EF5 || EF6 + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Core.Objects; #elif EFCORE using Microsoft.EntityFrameworkCore; @@ -38,6 +44,10 @@ public AuditConfiguration() ValueFormatterDictionary = new ConcurrentDictionary>(); } + public Func AuditEntryFactory { get; set; } + + public Func AuditEntryPropertyFactory { get; set; } + /// Gets or sets the automatic audit save pre action. /// The automatic audit save pre action. public Action AutoSavePreAction { get; set; } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntry.cs b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntry.cs index 00301715..babc1094 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntry.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntry.cs @@ -25,35 +25,59 @@ namespace Z.EntityFramework.Plus /// An audit entry. public class AuditEntry { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntry() +#if EF5 || EF6 + public void Build(Audit parent, ObjectStateEntry entry) +#else + public void Build(Audit parent, EntityEntry entry) +#endif { - } + if (CreatedBy == null) + { + CreatedBy = parent.CreatedBy; + } + + if (CreatedDate == DateTime.MinValue) + { + CreatedDate = DateTime.Now; + } + + if (Entry == null) + { + Entry = entry; + } + + if (Parent == null) + { + Parent = parent; + } + + if (Properties == null) + { + Properties = new List(); + } - /// Constructor. - /// The audit parent. - /// The object state entry. #if EF5 || EF6 - public AuditEntry(Audit parent, ObjectStateEntry entry) -#elif EFCORE - public AuditEntry(Audit parent, EntityEntry entry) + if (EntitySetName == null) + { + EntitySetName = entry.EntitySet.Name; + } #endif - { - CreatedBy = parent.CreatedBy; - CreatedDate = DateTime.Now; - Entry = entry; - Parent = parent; - Properties = new List(); #if EF5 || EF6 - EntitySetName = entry.EntitySet.Name; - if (!entry.IsRelationship) + + if (EntityTypeName == null) { - EntityTypeName = entry.Entity.GetType().Name; + if (!entry.IsRelationship) + { + EntityTypeName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name; + } } + #elif EFCORE - EntityTypeName = Entry.Entity.GetType().Name; + if (EntityTypeName == null) + { + EntityTypeName = Entry.Entity.GetType().Name; + } #endif } diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryFactoryArgs.cs b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryFactoryArgs.cs new file mode 100644 index 00000000..26f5d5db --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryFactoryArgs.cs @@ -0,0 +1,41 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryFactoryArgs + { +#if EF5 || EF6 + public AuditEntryFactoryArgs(Audit audit, ObjectStateEntry objectStateEntry, AuditEntryState auditEntryState) +#elif EFCORE + public AuditEntryFactoryArgs(Audit audit, EntityEntry entityEntry, AuditEntryState auditEntryState) +#endif + { + Audit = audit; + AuditEntryState = auditEntryState; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + + public Audit Audit { get; set; } + public AuditEntryState AuditEntryState { get; set; } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryProperty.cs b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryProperty.cs index 60134f9f..7de7b9dc 100644 --- a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryProperty.cs +++ b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryProperty.cs @@ -13,38 +13,40 @@ namespace Z.EntityFramework.Plus /// An audit entry property. public class AuditEntryProperty { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntryProperty() - { - } - /// Constructor. /// The audit entry parent. /// Name of the property audited. /// The old value audited. /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; + Build(parent, null, propertyName, oldValue, newValue); } - /// Constructor. - /// The audit entry parent. - /// The name of the relation audited. - /// Name of the property audited. - /// The old value audited. - /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; - RelationName = relationName; + InternalPropertyName = propertyName; + + if (!IsValueSet) + { + NewValue = newValue; + OldValue = oldValue; + } + + if (Parent == null) + { + Parent = parent; + } + + if (PropertyName == null) + { + PropertyName = propertyName; + } + + if (RelationName == null) + { + RelationName = relationName; + } } /// Gets or sets the identifier of the audit entry property. @@ -82,6 +84,15 @@ public AuditEntryProperty(AuditEntry parent, string relationName, string propert // EFCORE still have some issue with "NotMapped" attribute public object NewValue; #endif + /// Gets or sets a value indicating whether OldValue and NewValue is set. + /// true if OldValue and NewValue is set, false if not. + [NotMapped] + public bool IsValueSet { get; set; } + + /// Gets or sets the name of the property internally. + /// The name of the property internally. + [NotMapped] + public string InternalPropertyName { get; set; } /// Gets or sets the new value audited formatted. /// The new value audited formatted. diff --git a/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryPropertyArgs.cs b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryPropertyArgs.cs new file mode 100644 index 00000000..fe8ac325 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/Audit/AuditEntryPropertyArgs.cs @@ -0,0 +1,56 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryPropertyArgs + { +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string propertyName, object oldValue, object newValue) + : this(parent, objectStateEntry, null, propertyName, oldValue, newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string propertyName, object oldValue, object newValue) + : this(parent, entityEntry, null, propertyName, oldValue, newValue) +#endif + { + } + +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string relationName, string propertyName, object oldValue, object newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string relationName, string propertyName, object oldValue, object newValue) +#endif + { + AuditEntry = parent; + NewValue = newValue; + OldValue = oldValue; + PropertyName = propertyName; + RelationName = relationName; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + public AuditEntry AuditEntry { get; set; } + public string RelationName { get; set; } + public string PropertyName { get; set; } + public object OldValue { get; set; } + public object NewValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs index 375ffe8d..e0627f20 100644 --- a/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EF6/Properties/AssemblyInfo.cs @@ -18,5 +18,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("89f47e22-ff83-4ac8-be90-c6755748cf89")] -[assembly: AssemblyVersion("1.3.12")] -[assembly: AssemblyFileVersion("1.3.12")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0")] +[assembly: AssemblyFileVersion("1.4.0")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilter.cs index 8d77aa62..ef47eb5d 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; namespace Z.EntityFramework.Plus @@ -101,4 +102,5 @@ public virtual BaseQueryFilter Clone(QueryFilterContext filterContext) throw new Exception(ExceptionMessage.GeneralException); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilterQueryable.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilterQueryable.cs index 4d1eee71..545cbc3c 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilterQueryable.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/BaseQueryFilterQueryable.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; #if EF5 || EF6 @@ -63,4 +64,5 @@ public virtual void UpdateInternalQuery() throw new Exception(ExceptionMessage.GeneralException); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/DbContext.Filter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/DbContext.Filter.cs index 8672b5a4..74f1e7a8 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/DbContext.Filter.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/DbContext.Filter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; @@ -65,4 +66,5 @@ public static BaseQueryFilter Filter(this DbContext context, object key, Func return filter; } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.AsNoFilter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.AsNoFilter.cs index f7fcf22e..52a6b5b3 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.AsNoFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.AsNoFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Linq; #if EF5 || EF6 using System.Data.Entity; @@ -32,4 +33,5 @@ public static IQueryable AsNoFilter(this DbSet query) where T : class return queryFilterQueryable != null ? (IQueryable)queryFilterQueryable.OriginalQuery : query; } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.Filter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.Filter.cs index 53d78a7a..08a4a987 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.Filter.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/Extensions/IDbSet.Filter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Linq; #if EF5 || EF6 using System.Data.Entity; @@ -40,4 +41,5 @@ public static IQueryable Filter(this DbSet query, params object[] keys) return filterContext.ApplyFilter(query, keys); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilter.cs index 6a9f09b0..b3a923dd 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilter.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilter.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; @@ -57,4 +58,5 @@ public override BaseQueryFilter Clone(QueryFilterContext filterContext) return new QueryFilter(filterContext, Filter); } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterContext.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterContext.cs index 54011a11..bbda72a2 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterContext.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterContext.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; using System.Linq; @@ -222,4 +223,5 @@ public void LoadGenericContextInfo(DbContext context) } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterManager.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterManager.cs index b93a930c..579c88ce 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterManager.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterManager.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Collections.Generic; using System.Linq; @@ -174,4 +175,5 @@ public static void InitilizeGlobalFilter(DbContext context) } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterQueryable.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterQueryable.cs index 8d182e3b..f3bbc746 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterQueryable.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterQueryable.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System.Collections.Generic; using System.Linq; #if EF5 @@ -59,4 +60,5 @@ public override void UpdateInternalQuery() #endif } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterSet.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterSet.cs index f04d8c53..a48f8e0c 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterSet.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilter/QueryFilterSet.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if !EF6 using System; using System.Linq; using System.Linq.Expressions; @@ -191,4 +192,5 @@ public void UpdateInternalQuery(DbContext context, object query) } #endif } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs new file mode 100644 index 00000000..23106a44 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/BaseQueryFilterInterceptor.cs @@ -0,0 +1,154 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus +{ + /// A base class for query filter. + public abstract class BaseQueryFilterInterceptor + { + /// The unique key. + public readonly string UniqueKey = Guid.NewGuid().ToString(); + + /// List of is filter enabled. + public List> IsFilterEnabledList = new List>(); + + /// Gets or sets the context that owns this item. + /// The owner context. + public QueryFilterContextInterceptor OwnerContext { get; set; } + + /// Gets or sets a value indicating whether the filter is enabled by default. + /// true if the filter is enabled by default, false if not. + public bool IsDefaultEnabled { get; set; } + + /// Gets or sets the type of the element. + /// The type of the element. + public Type ElementType { get; set; } + + /// Gets database expression. + /// The context. + /// The type. + /// The database expression. + public abstract DbExpression GetDbExpression(DbContext context, Type type); + + /// Disables this filter. + public void Disable() + { + Disable(null); + } + + /// Disables this filter on the speficied type. + /// Type of the element to disable the filter on. + public void Disable() + { + Disable(typeof(TType)); + } + + /// Disable this filter on the specified types. + /// A variable-length parameters list containing types to disable the filter on. + public void Disable(params Type[] types) + { + if (types == null || types.Length == 0) + { + if (IsFilterEnabledList.Count == 0) + { + IsDefaultEnabled = false; + } + else + { + IsFilterEnabledList.Add(type1 => false); + } + } + else + { + foreach (var type in types) + { + IsFilterEnabledList.Add(type1 => type.IsAssignableFrom(type1) ? false : (bool?)null); + } + } + + if (OwnerContext != null) + { + OwnerContext.ClearCache(); + } + else + { + QueryFilterManager.ClearAllCache(); + } + } + + /// Enables this filter. + public void Enable() + { + Enable(null); + } + + /// Enables this filter on the speficied type. + /// Type of the element to enable the filter on. + public void Enable() + { + Enable(typeof(TType)); + } + + /// Enables this filter on the specified types. + /// A variable-length parameters list containing types to enable the filter on. + public void Enable(params Type[] types) + { + if (types == null || types.Length == 0) + { + if (IsFilterEnabledList.Count == 0) + { + IsDefaultEnabled = true; + } + else + { + IsFilterEnabledList.Add(type1 => true); + } + } + else + { + foreach (var type in types) + { + IsFilterEnabledList.Add(type1 => type.IsAssignableFrom(type1) ? true : (bool?)null); + } + } + + if (OwnerContext != null) + { + OwnerContext.ClearCache(); + } + else + { + QueryFilterManager.ClearAllCache(); + } + } + + /// Queries if a type is enabled. + /// The type. + /// true if a type is enabled, false if not. + public bool IsTypeEnabled(Type type) + { + var isEnabled = IsDefaultEnabled; + + foreach (var isFiltedEnabled in IsFilterEnabledList) + { + var isEnabledFactory = isFiltedEnabled(type); + + if (isEnabledFactory.HasValue) + { + isEnabled = isEnabledFactory.Value; + } + } + + return isEnabled; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/DbContext.Filter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/DbContext.Filter.cs new file mode 100644 index 00000000..2da236d1 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/DbContext.Filter.cs @@ -0,0 +1,61 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Data.Entity; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// Gets the filter associated with the specified key from the context. + /// The context filtered. + /// The filter key associated to the filter. + /// The filter associated with the specified key from the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, object key) + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(context); + return filterContext.GetFilter(key); + } + + /// + /// Creates and return a filter added for the context. + /// + /// The type of elements of the query. + /// The context to filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + return context.Filter(Guid.NewGuid(), queryFilter, isEnabled); + } + + /// + /// Creates and return a filter associated with the specified key added for the context. + /// + /// The type of elements of the query. + /// The context filtered. + /// The filter key associated to the filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(this DbContext context, object key, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(context); + var filter = filterContext.AddFilter(key, queryFilter); + + if (isEnabled) + { + filter.Enable(); + } + + return filter; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs new file mode 100644 index 00000000..a5eef715 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.AsNoFilter.cs @@ -0,0 +1,24 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Data.Entity; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// Return the orginal query before the context was filtered. + /// The type of elements of the query. + /// The filtered query from which the original query should be retrieved. + /// The orginal query before the context was filtered. + public static IQueryable AsNoFilter(this IDbSet query) where T : class + { + return QueryFilterManager.HookFilter(query, QueryFilterManager.DisableAllFilter); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs new file mode 100644 index 00000000..98718ce2 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/Extensions/IDbSet.Filter.cs @@ -0,0 +1,50 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Data.Entity; +using System.Linq; +using System.Text; + +namespace Z.EntityFramework.Plus +{ + public static partial class QueryInterceptorFilterExtensions + { + /// + /// Filter the query using context filters associated with specified keys. + /// + /// The type of elements of the query. + /// The query to filter using context filters associated with specified keys. + /// + /// A variable-length parameters list containing keys associated to context filters to use to filter the + /// query. + /// + /// The query filtered using context filters associated with specified keys. + public static IQueryable Filter(this IDbSet query, params object[] keys) where T : class + { + var filterContext = QueryFilterManager.AddOrGetFilterContext(query.GetDbContext()); + var filterHook = QueryFilterManager.EnableFilterById; + + var sb = new StringBuilder(); + + if (keys != null) + { + foreach (var key in keys) + { + var filter = filterContext.GetFilter(key); + if (filter == null) + { + continue; + } + sb.Append(filter.UniqueKey); + sb.Append(";"); + } + } + + return QueryFilterManager.HookFilter(query.AsNoFilter(), filterHook + sb); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptor.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptor.cs new file mode 100644 index 00000000..07748b42 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptor.cs @@ -0,0 +1,62 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + /// A class for query filter. + /// The type of the filter element. + public class QueryFilterInterceptor : BaseQueryFilterInterceptor where T : class + { + /// Constructor. + /// The filter. + public QueryFilterInterceptor(Func, IQueryable> filter) + { + ElementType = typeof(T); + Filter = filter; + } + + /// Gets or sets the filter. + /// The filter. + public Func, IQueryable> Filter { get; set; } + + /// Gets database expression. + /// The context. + /// The type. + /// The database expression. + public override DbExpression GetDbExpression(DbContext context, Type type) + { + var contextFullName = context.GetType().AssemblyQualifiedName ?? context.GetType().FullName; + var typeFullName = type.AssemblyQualifiedName ?? type.FullName; + var hookId = QueryFilterManager.PrefixHook + contextFullName + ";" + typeFullName + ";" + UniqueKey; + + if (!QueryFilterManager.DbExpressionByHook.ContainsKey(hookId)) + { + // CREATE set + var setMethod = typeof(DbContext).GetMethod("Set", new Type[0]).MakeGenericMethod(type); + var dbSet = (IQueryable)setMethod.Invoke(context, null); + + // APPLY filter + dbSet = Filter(dbSet); + + // APPLY hook + dbSet = QueryFilterManager.HookFilter(dbSet, hookId); + + // Hook the filter + var objectQuery = dbSet.GetObjectQuery(); + objectQuery.ToTraceString(); + } + + // TODO: WeakTable ? + return QueryFilterManager.DbExpressionByHook[hookId]; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorApply.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorApply.cs new file mode 100644 index 00000000..b8d9ac91 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorApply.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +namespace Z.EntityFramework.Plus +{ + public class QueryFilterInterceptorApply + { + /// List of apply filters. + public List> ApplyFilterList = new List>(); + + /// The global filters. + public QueryFilterContextInterceptor GlobalFilters; + + /// The instance filters. + public QueryFilterContextInterceptor InstanceFilters; + + /// Query if 'filter' is enabled. + /// Specifies the filter. + /// true if enabled, false if not. + public bool? IsEnabled(BaseQueryFilterInterceptor filter) + { + bool? isEnabled = null; + + foreach (var applyFilter in ApplyFilterList) + { + var shouldEnable = applyFilter(filter); + + if (shouldEnable.HasValue) + { + isEnabled = shouldEnable.Value; + } + } + + return isEnabled; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorContext.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorContext.cs new file mode 100644 index 00000000..b6a3d6d2 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorContext.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Metadata.Edm; +using System.Data.Entity.Infrastructure; +using System.Linq; +using System.Reflection; + +namespace Z.EntityFramework.Plus +{ + public class QueryFilterContextInterceptor + { + /// true if clear cache required. + public bool ClearCacheRequired; + + /// The filter by entity set base. + public Dictionary> FilterByEntitySetBase = new Dictionary>(); + + /// Gets or sets the filters. + /// The filters. + public ConcurrentDictionary FilterByKey = new ConcurrentDictionary(); + + /// Type of the filter by. + public ConcurrentDictionary> FilterByType = new ConcurrentDictionary>(); + + /// Gets or sets the filters. + /// The filters. + public ConcurrentDictionary GlobalFilterByKey = new ConcurrentDictionary(); + + /// Type of the filter by. + public ConcurrentDictionary> GlobalFilterByType = new ConcurrentDictionary>(); + + /// Set the type by database belongs to. + public Dictionary> TypeByDbSet = new Dictionary>(); + + /// The type by entity set base. + public Dictionary TypeByEntitySetBase = new Dictionary(); + + public QueryFilterContextInterceptor(DbContext context) + { + Context = context; + Initialize(context); + } + + /// Gets or sets the context associated with the filter context. + /// The context associated with the filter context. + public DbContext Context { get; set; } + + /// Gets applicable filter. + /// Name of the database set. + /// The applicable filter. + public List GetApplicableFilter(string dbSetName) + { + var list = new List(); + + var types = TypeByDbSet[dbSetName]; + + foreach (var type in types) + { + List filterList; + if (FilterByType.TryGetValue(type, out filterList)) + { + list.AddRange(filterList); + } + } + + return list; + } + + /// Gets global applicable filter. + /// Name of the database set. + /// The global applicable filter. + public List GetGlobalApplicableFilter(string dbSetName) + { + var list = new List(); + + var types = TypeByDbSet[dbSetName]; + + foreach (var type in types) + { + List filterList; + if (GlobalFilterByType.TryGetValue(type, out filterList)) + { + list.AddRange(filterList); + } + } + + return list; + } + + /// Adds a query filter to the filter context associated with the specified key. + /// The type of elements of the query. + /// The filter key. + /// The filter. + /// The query filter added to the filter context associated with the specified ke . + public BaseQueryFilterInterceptor AddFilter(object key, Func, IQueryable> filter) where T : class + { + var queryFilter = new QueryFilterInterceptor(filter); + queryFilter.OwnerContext = this; + + // FilterByKey + { + FilterByKey.AddOrUpdate(key, queryFilter, (o, interceptorFilter) => queryFilter); + } + + // FilterByType + { + if (!FilterByType.ContainsKey(typeof (T))) + { + FilterByType.TryAdd(typeof (T), new List()); + } + + FilterByType[typeof (T)].Add(queryFilter); + } + + ClearCache(); + return queryFilter; + } + + /// Clears the cache. + public void ClearCache() + { + if (ClearCacheRequired) + { + QueryFilterManager.ClearQueryCache(Context); + ClearCacheRequired = false; + } + } + + /// Gets the filter associated to the specified key. + /// The filter key. + /// The filter associated to the specified key. + public BaseQueryFilterInterceptor GetFilter(object key) + { + BaseQueryFilterInterceptor filter; + if (!FilterByKey.TryGetValue(key, out filter)) + { + GlobalFilterByKey.TryGetValue(key, out filter); + } + return filter; + } + + /// Initializes this object. + /// The context. + private void Initialize(DbContext context) + { + // GET DbSet<> properties + var setProperties = context.GetDbSetProperties(); + + foreach (var setProperty in setProperties) + { + // GET DbSet<> + var dbSet = (IQueryable) setProperty.GetValue(context, null); + + // DbSet<>.InternalQuery + var internalQueryProperty = typeof (DbQuery<>).MakeGenericType(dbSet.ElementType).GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbSet, null); + + // DbSet<>.InternalQuery.EntitySet + var entitySetProperty = internalQuery.GetType().GetProperty("EntitySet", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var entitySet = (EntitySet) entitySetProperty.GetValue(internalQuery, null); + + var elementType = dbSet.ElementType; + var entityTypebase = entitySet.ElementType.FullName; + + // TypeByEntitySetBase + { + if (!TypeByEntitySetBase.ContainsKey(entityTypebase)) + { + TypeByEntitySetBase.Add(entityTypebase, elementType); + } + } + + // TypeByDbSet + { + var baseType = elementType; + + var types = new List(); + while (baseType != null && baseType != typeof (object)) + { + types.Add(baseType); + + // LINK interface + var interfaces = baseType.GetInterfaces(); + foreach (var @interface in interfaces) + { + types.Add(@interface); + } + + baseType = baseType.BaseType; + } + + // ENSURE all discting + types = types.Distinct().ToList(); + + if (!TypeByDbSet.ContainsKey(entityTypebase)) + { + TypeByDbSet.Add(entityTypebase, types); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs new file mode 100644 index 00000000..ce17810c --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbCommandTree.cs @@ -0,0 +1,89 @@ +using System; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Infrastructure.Interception; +using System.Linq; + +namespace Z.EntityFramework.Plus +{ + /// A query interceptor filter database command tree. + public class QueryFilterInterceptorDbCommandTree : IDbCommandTreeInterceptor + { + /// + /// This method is called after a new + /// has been created. + /// The tree that is used after interception can be changed by setting + /// + /// while intercepting. + /// + /// Contextual information associated with the call. + public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) + { + var dbQueryCommandTree = interceptionContext.Result as DbQueryCommandTree; + if (dbQueryCommandTree != null && interceptionContext.DbContexts.Count() == 1) + { + var context = interceptionContext.DbContexts.First(); + + // Visit first to find filter ID && hook + var visitorFilter = new QueryFilterInterceptorDbFilterExpression(); + var queryFiltered = dbQueryCommandTree.Query.Accept(visitorFilter); + + if (!string.IsNullOrEmpty(visitorFilter.HookID)) + { + if (!QueryFilterManager.DbExpressionByHook.ContainsKey(visitorFilter.HookID)) + { + QueryFilterManager.DbExpressionByHook.TryAdd(visitorFilter.HookID, queryFiltered); + } + } + else + { + var filterByContext = QueryFilterManager.AddOrGetFilterContext(context); + filterByContext.ClearCacheRequired = true; + + var filterQuery = new QueryFilterInterceptorApply + { + InstanceFilters = filterByContext + }; + + if (visitorFilter.FilterID != null && visitorFilter.FilterID.Count > 0) + { + foreach (var filter in visitorFilter.FilterID) + { + if (filter == QueryFilterManager.DisableAllFilter) + { + // Disable all filter in the context! + filterQuery.ApplyFilterList.Add(interceptorFilter => false); + } + else if (filter.StartsWith(QueryFilterManager.EnableFilterById, StringComparison.InvariantCulture)) + { + // Enable all specific filter + var filters = filter.Substring(QueryFilterManager.EnableFilterById.Length).Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries); + + if (filters.Length == 0) + { + filterQuery.ApplyFilterList.Add(interceptorFilter => false); + } + foreach (var applyFilter in filters) + { + filterQuery.ApplyFilterList.Add(interceptorFilter => interceptorFilter.UniqueKey.ToString() == applyFilter ? true : (bool?)null); + } + } + } + } + + // VISIT filter + var visitor = new QueryFilterInterceptorDbScanExpression + { + Context = context, + InstanceFilterContext = filterByContext, + FilterQuery = filterQuery + }; + + var newQuery = queryFiltered.Accept(visitor); + + // CREATE a new Query + interceptionContext.Result = new DbQueryCommandTree(dbQueryCommandTree.MetadataWorkspace, dbQueryCommandTree.DataSpace, newQuery, true); + } + } + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs new file mode 100644 index 00000000..d5cd00f7 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbFilterExpression.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus +{ + /// A query interceptor filter database filter expression. + public class QueryFilterInterceptorDbFilterExpression : DefaultExpressionVisitor + { + /// Identifier for the filter. + public List FilterID; + + /// Identifier for the hook. + public string HookID; + + /// + /// Implements the visitor pattern for a predicate applied to filter an input set. + /// + /// The filter expression. + /// The implemented visitor. + public override DbExpression Visit(DbFilterExpression expression) + { + var predicate = expression.Predicate; + var comparison = predicate as DbComparisonExpression; + + if (comparison != null) + { + var leftConstant = comparison.Left as DbConstantExpression; + + if (leftConstant != null) + { + { + var valueString = leftConstant.Value as string; + + if (valueString != null) + { + if (valueString.StartsWith(QueryFilterManager.PrefixFilter, StringComparison.InvariantCulture)) + { + if (FilterID == null) + { + FilterID = new List(); + } + FilterID.Add(valueString); + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + if (valueString.StartsWith(QueryFilterManager.PrefixHook, StringComparison.InvariantCulture)) + { + HookID = valueString; + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + } + } + } + + var rightConstant = comparison.Right as DbConstantExpression; + + if (rightConstant != null) + { + { + var valueString = rightConstant.Value as string; + + if (valueString != null) + { + if (valueString.StartsWith(QueryFilterManager.PrefixFilter, StringComparison.InvariantCulture)) + { + if (FilterID == null) + { + FilterID = new List(); + } + FilterID.Add(valueString); + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + if (valueString.StartsWith(QueryFilterManager.PrefixHook, StringComparison.InvariantCulture)) + { + HookID = valueString; + + // It's a fake filter! Do nothing + return expression.Input.Expression; + } + } + } + } + } + + var baseExpression = base.Visit(expression); + return baseExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs new file mode 100644 index 00000000..950a8061 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbProjectExpression.cs @@ -0,0 +1,44 @@ +using System.Data.Entity.Core.Common.CommandTrees; + +namespace Z.EntityFramework.Plus.QueryInterceptorFilter +{ + /// A database project to input expression visitor. + public class QueryFilterInterceptorDbProjectExpression : DefaultExpressionVisitor + { + /// The database scan expression. + public DbExpression DbScanExpression; + + /// + /// Implements the visitor pattern for the projection of a given input set over the specified + /// expression. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbProjectExpression expression) + { + var baseExpression = base.Visit(expression); + var baseDbProject = baseExpression as DbProjectExpression; + + if (baseDbProject != null) + { + return baseDbProject.Input.Expression; + } + + + // This situation may happen when another user-defined interceptor is used + // The library may not be compatible in this situation + return baseExpression; + } + + /// + /// Implements the visitor pattern for a scan over an entity set or relationship set, as + /// indicated by the Target property. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbScanExpression expression) + { + return DbScanExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs new file mode 100644 index 00000000..d33d25cf --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorDbScanExpression.cs @@ -0,0 +1,131 @@ +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Core.Metadata.Edm; +using Z.EntityFramework.Plus.QueryInterceptorFilter; + +namespace Z.EntityFramework.Plus +{ + /// A database scan expression visitor. + public class QueryFilterInterceptorDbScanExpression : DefaultExpressionVisitor + { + /// The context. + public DbContext Context; + + /// The filtered expression. + public DbExpression FilteredExpression; + + /// The filter query. + public QueryFilterInterceptorApply FilterQuery; + + /// Context for the instance filter. + public QueryFilterContextInterceptor InstanceFilterContext; + + /// + /// Implements the visitor pattern for a scan over an entity set or relationship set, as + /// indicated by the Target property. + /// + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbScanExpression expression) + { + var baseExpression = base.Visit(expression); + var baseType = expression.Target.ElementType; + var fullName = baseType.FullName; + + baseExpression = ApplyFilter(baseExpression, fullName); + + return baseExpression; + } + + /// Implements the visitor pattern for retrieving an instance property. + /// The expression. + /// The implemented visitor. + public override DbExpression Visit(DbPropertyExpression expression) + { + var baseExpression = base.Visit(expression); + var baseExpressionProperty = baseExpression as DbPropertyExpression; + + if (baseExpressionProperty == null) + { + return baseExpression; + } + + var navProp = baseExpressionProperty.Property as NavigationProperty; + if (navProp != null && baseExpression.ResultType.ToString().Contains("Transient.collection[")) + { + var targetEntityType = navProp.ToEndMember.GetEntityType(); + var fullName = targetEntityType.FullName; + + baseExpression = ApplyFilter(baseExpression, fullName); + } + + return baseExpression; + } + + /// Applies the filter. + /// The base expression. + /// Name of the full. + /// A DbExpression. + public DbExpression ApplyFilter(DbExpression baseExpression, string fullName) + { + if (InstanceFilterContext.TypeByEntitySetBase.ContainsKey(fullName)) + { + var filters = InstanceFilterContext.GetGlobalApplicableFilter(fullName); + var type = InstanceFilterContext.TypeByEntitySetBase[fullName]; + + if (filters.Count > 0) + { + foreach (var filter in filters) + { + var filterQueryEnabled = FilterQuery.IsEnabled(filter); + if ((filterQueryEnabled.HasValue && !filterQueryEnabled.Value) + || (!filterQueryEnabled.HasValue && !filter.IsTypeEnabled(type))) + { + continue; + } + + var expression2 = filter.GetDbExpression(Context, type); + + if (expression2 != null) + { + var visitor = new QueryFilterInterceptorDbProjectExpression(); + visitor.DbScanExpression = baseExpression; + var filetered = expression2.Accept(visitor); + baseExpression = filetered; + } + } + } + } + if (InstanceFilterContext.TypeByEntitySetBase.ContainsKey(fullName)) + { + var filters = InstanceFilterContext.GetApplicableFilter(fullName); + var type = InstanceFilterContext.TypeByEntitySetBase[fullName]; + + if (filters.Count > 0) + { + foreach (var filter in filters) + { + var filterQueryEnabled = FilterQuery.IsEnabled(filter); + if ((filterQueryEnabled.HasValue && !filterQueryEnabled.Value) + || (!filterQueryEnabled.HasValue && !filter.IsTypeEnabled(type))) + { + continue; + } + + var expression2 = filter.GetDbExpression(Context, type); + + if (expression2 != null) + { + var visitor = new QueryFilterInterceptorDbProjectExpression(); + visitor.DbScanExpression = baseExpression; + var filetered = expression2.Accept(visitor); + baseExpression = filetered; + } + } + } + } + + return baseExpression; + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorManager.cs b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorManager.cs new file mode 100644 index 00000000..f0427fd1 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EF6/QueryFilterInterceptor/QueryFilterInterceptorManager.cs @@ -0,0 +1,221 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Data.Entity; +using System.Data.Entity.Core.Common.CommandTrees; +using System.Data.Entity.Infrastructure.Interception; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace Z.EntityFramework.Plus +{ + /// A class to manage query interceptor filter options. + public static class QueryFilterManager + { + /// A filter specifying the prefix. + internal static string PrefixFilter = "Z.EntityFramework.Plus.QueryFilterInterceptor.Filer;"; + + /// The prefix hook. + internal static string PrefixHook = "Z.EntityFramework.Plus.QueryFilterInterceptor.Hook;"; + + /// A filter specifying the disable all. + internal static string DisableAllFilter = PrefixFilter + "DisableAll;"; + + /// Identifier for the enable filter by. + internal static string EnableFilterById = PrefixFilter + "EnableById;"; + + /// Static constructor. + static QueryFilterManager() + { + DbInterception.Add(new QueryFilterInterceptorDbCommandTree()); + + CacheWeakFilterContext = new ConditionalWeakTable(); + GlobalFiltersByKey = new ConcurrentDictionary(); + GlobalFilterByType = new ConcurrentDictionary>(); + DbExpressionByHook = new ConcurrentDictionary(); + } + + /// Gets the database expression by hook. + /// The database expression by hook. + public static ConcurrentDictionary DbExpressionByHook { get; } + + /// Gets the global filters. + /// The global filters. + public static ConcurrentDictionary GlobalFiltersByKey { get; } + + /// Gets or sets the type of the global filter by. + /// The type of the global filter by. + public static ConcurrentDictionary> GlobalFilterByType { get; set; } + + /// Gets or sets the weak table containing filter context for a specified context. + /// The weak table containing filter context for a specified context. + public static ConditionalWeakTable CacheWeakFilterContext { get; set; } + + /// Gets the filter associated with the specified key from the context. + /// The filter key associated to the filter. + /// The filter associated with the specified key from the context. + public static BaseQueryFilterInterceptor Filter(object key) + { + BaseQueryFilterInterceptor filter; + GlobalFiltersByKey.TryGetValue(key, out filter); + + return filter; + } + + /// + /// Creates and return a filter added for the context. + /// + /// The type of elements of the query. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + return Filter(Guid.NewGuid(), queryFilter, isEnabled); + } + + /// + /// Creates and return a filter associated with the specified key added for the context. + /// + /// The type of elements of the query. + /// The filter key associated to the filter. + /// The query filter to apply to the the context. + /// true if the filter is enabled. + /// The filter created and added to the the context. + public static BaseQueryFilterInterceptor Filter(object key, Func, IQueryable> queryFilter, bool isEnabled = true) where T : class + { + BaseQueryFilterInterceptor filter; + + // FilterByKey + { + if (!GlobalFiltersByKey.TryGetValue(key, out filter)) + { + filter = new QueryFilterInterceptor(queryFilter) {IsDefaultEnabled = isEnabled}; + GlobalFiltersByKey.AddOrUpdate(key, filter, (o, interceptorFilter) => filter); + } + } + + // FilterByType + { + if (!GlobalFilterByType.ContainsKey(typeof (T))) + { + GlobalFilterByType.AddOrUpdate(typeof (T), new List(), (type, list) => list); + } + + GlobalFilterByType[typeof (T)].Add(filter); + } + + ClearAllCache(); + + return filter; + } + + /// Adds or get the filter context associated with the context. + /// The context associated with the filter context. + /// The filter context associated with the context. + public static QueryFilterContextInterceptor AddOrGetFilterContext(DbContext context) + { + QueryFilterContextInterceptor filterContext; + + if (!CacheWeakFilterContext.TryGetValue(context, out filterContext)) + { + filterContext = new QueryFilterContextInterceptor(context) + { + GlobalFilterByKey = GlobalFiltersByKey, + GlobalFilterByType = GlobalFilterByType + }; + CacheWeakFilterContext.Add(context, filterContext); + } + + return filterContext; + } + + /// Hook filter. + /// Generic type parameter. + /// The query. + /// The value. + /// An IQueryable<T> + public static IQueryable HookFilter(IQueryable query, string value) + { + // CREATE hook + var parameter = Expression.Parameter(typeof (T)); + var left = Expression.Constant(value); + var right = Expression.Constant(value); + var predicate = Expression.Equal(left, right); + var lambda = Expression.Lambda>(predicate, parameter); + + // APPLY hook + query = query.Where(lambda); + + return query; + } + + /// Initilize global filter in the context. + /// The context to initialize global filter on. + public static void InitilizeGlobalFilter(DbContext context) + { + // NOT anymore required! Remove this on v2? + } + + /// Clears the query cache described by context. + /// The context to initialize global filter on. + public static void ClearQueryCache(DbContext context) + { + try + { + var objectContext = context.GetObjectContext(); + var metaWorkspace = objectContext.MetadataWorkspace; + + var getQueryCacheManagerMethod = metaWorkspace.GetType().GetMethod("GetQueryCacheManager", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var getQueryCacheManager = getQueryCacheManagerMethod.Invoke(metaWorkspace, null); + + var clearMethod = getQueryCacheManager.GetType().GetMethod("Clear", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + clearMethod.Invoke(getQueryCacheManager, null); + + // GET DbSet<> properties + var setProperties = context.GetDbSetProperties(); + + foreach (var setProperty in setProperties) + { + var dbSet = (IQueryable) setProperty.GetValue(context, null); + + // Disable Set Caching + { + var objectQuery = dbSet.GetObjectQuery(); + + var stateField = objectQuery.GetType().BaseType.GetField("_state", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var state = stateField.GetValue(objectQuery); + + var cachedPlanField = state.GetType().GetField("_cachedPlan", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + cachedPlanField.SetValue(state, null); + } + } + } + catch (Exception) + { + // TODO: Find a better way to handle this! + } + } + + /// Clears all cache. + public static void ClearAllCache() + { + var propertyValues = CacheWeakFilterContext.GetType().GetProperty("Values", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var values = (List) propertyValues.GetValue(CacheWeakFilterContext, null); + + foreach (var value in values) + { + value.ClearCache(); + } + } + + /// Clears the global filter. + public static void ClearGlobalFilter() + { + GlobalFiltersByKey.Clear(); + GlobalFilterByType.Clear(); + } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/BaseQueryIncludeOptimizedChild.cs b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/BaseQueryIncludeOptimizedChild.cs index 33ca3821..51e978ec 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/BaseQueryIncludeOptimizedChild.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/BaseQueryIncludeOptimizedChild.cs @@ -7,6 +7,7 @@ using System; using System.Linq; +using System.Linq.Expressions; namespace Z.EntityFramework.Plus { @@ -19,5 +20,7 @@ public virtual void CreateIncludeQuery(IQueryable rootQuery) { throw new Exception(ExceptionMessage.GeneralException); } + + public abstract Expression GetFilter(); } } \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/Extensions/IQueryable`.IncludeOptimized.cs b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/Extensions/IQueryable`.IncludeOptimized.cs index 4a5015a7..c173178f 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/Extensions/IQueryable`.IncludeOptimized.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/Extensions/IQueryable`.IncludeOptimized.cs @@ -14,6 +14,18 @@ namespace Z.EntityFramework.Plus { public static class QueryIncludeOptimizedExtensions { + //public static IQueryable InternalIncludeOptimized(this IQueryable query, Expression>> queryIncludeFilter) where T : class where TChild : class + //{ + // // GET query root + // var includeOrderedQueryable = query as QueryIncludeOptimizedParentQueryable ?? new QueryIncludeOptimizedParentQueryable(query); + + // // ADD sub query + // includeOrderedQueryable.Childs.Add(new QueryIncludeOptimizedChild(queryIncludeFilter)); + + // // RETURN root + // return includeOrderedQueryable; + //} + /// /// An IQueryable<T> extension method that include and filter related entities with a optimized SQL. /// diff --git a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedChild`2.cs b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedChild`2.cs index b394fb50..45edf5d3 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedChild`2.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedChild`2.cs @@ -73,5 +73,10 @@ public override void CreateIncludeQuery(IQueryable rootQuery) } } } + + public override Expression GetFilter() + { + return Filter != null ? (Expression)Filter : FilterSingle; + } } } \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedParentQueryable`.cs b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedParentQueryable`.cs index 08a6ccc7..b816f20d 100644 --- a/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedParentQueryable`.cs +++ b/src/Z.EntityFramework.Plus.EF6/QueryIncludeOptimized/QueryIncludeOptimizedParentQueryable`.cs @@ -8,8 +8,10 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Linq.Expressions; +using System.Reflection; using System.Threading.Tasks; #if EF5 using System.Data.Metadata.Edm; @@ -125,9 +127,66 @@ public IEnumerable CreateEnumerable() } // RESOLVE current and all future child queries - return QueryIncludeOptimizedManager.AllowQueryBatch ? newQuery.Future().ToList() : newQuery.ToList(); + var list = QueryIncludeOptimizedManager.AllowQueryBatch ? newQuery.Future().ToList() : newQuery.ToList(); + + //foreach (var child in Childs) + //{ + // // GET all paths + // var visitor = new SelectPathVisitor(); + // visitor.Visit(child.GetFilter()); + + // var paths = visitor.paths; + + // if (paths.Count > 0) + // { + + // foreach (var item in list) + // { + // foreach (var path in paths) + // { + // var property = item.GetType().GetProperty(path, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + + // if (property != null) + // { + // var value = property.GetValue(path); + // } + // } + // } + + // } + //} + + return list; } + //public class SelectPathVisitor : ExpressionVisitor + //{ + // public List paths = new List(); + + // public override Expression Visit(Expression node) + // { + // if (node != null && node.NodeType == ExpressionType.Call) + // { + // var methodCall = node as MethodCallExpression; + + // if (methodCall != null + // && methodCall.Method.ReflectedType != null + // && methodCall.Method.ReflectedType.FullName == "System.Linq.Enumerable" + // && (methodCall.Method.Name == "Select" + // || methodCall.Method.Name == "SelectMany")) + // { + // var argument = methodCall.Arguments[0]; + // var s = argument.ToString(); + + // // x.Rights.Where(...) + // var path = s.Split('.')[1]; + // paths.Add(path); + // } + // } + // return base.Visit(node); + // } + //} + /// Creates the queryable. /// Thrown when an exception error condition occurs. /// The new queryable. diff --git a/src/Z.EntityFramework.Plus.EF6/Z.EntityFramework.Plus.EF6.csproj b/src/Z.EntityFramework.Plus.EF6/Z.EntityFramework.Plus.EF6.csproj index dc363d42..a6d3180f 100644 --- a/src/Z.EntityFramework.Plus.EF6/Z.EntityFramework.Plus.EF6.csproj +++ b/src/Z.EntityFramework.Plus.EF6/Z.EntityFramework.Plus.EF6.csproj @@ -38,8 +38,10 @@ - ..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll - False + ..\..\zEntityFramework\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll + + + ..\..\zEntityFramework\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll @@ -66,6 +68,8 @@ + + @@ -151,6 +155,18 @@ + + + + + + + + + + + + diff --git a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs index d2220296..09074591 100644 --- a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs +++ b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL +#if FULL || QUERY_FILTER #if EF5 || EF6 #if EF5 diff --git a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs index c3c116a7..e67d316b 100644 --- a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE +#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE || QUERY_FILTER #if EF5 || EF6 using System; using System.Data.Entity; diff --git a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs index 757fc9c4..88f1426b 100644 --- a/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EF6/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED #if EF5 || EF6 using System; using System.Data.Entity.Infrastructure; @@ -41,17 +41,33 @@ internal static ObjectQuery GetObjectQuery(this IQueryable query) if (dbQuery == null) { - throw new Exception(ExceptionMessage.GeneralException); + var internalQueryProperty = query.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + + if (internalQueryProperty == null) + { + throw new Exception(ExceptionMessage.GeneralException); + } + + var internalQuery = internalQueryProperty.GetValue(query, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; + + return objectQuery; } - var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); - var internalQuery = internalQueryProperty.GetValue(dbQuery, null); - var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); - var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + { + var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbQuery, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; - objectQuery = objectQueryContext as ObjectQuery; + return objectQuery; + } - return objectQuery; } } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit.cs index 4828f3cd..20f57234 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit.cs @@ -28,6 +28,23 @@ public Audit() { _configuration = new Lazy(() => AuditManager.DefaultConfiguration.Clone()); Entries = new List(); + + try + { +#if !NETSTANDARD1_3 + CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; +#endif + + if (string.IsNullOrEmpty(CreatedBy)) + { + CreatedBy = "System"; + } + } + catch (Exception) + { + // Oops! it's k, this is the responsability of the user to set the default CreatedBy field + CreatedBy = "System"; + } } /// Gets or sets the entries. diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityAdded.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityAdded.cs index ce2e47b4..9c0eb54e 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityAdded.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityAdded.cs @@ -7,11 +7,14 @@ #if EF5 using System.Data.Objects; +using System.Linq; #elif EF6 using System.Data.Entity.Core.Objects; +using System.Linq; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -29,11 +32,12 @@ public static void AuditEntityAdded(Audit audit, ObjectStateEntry objectStateEnt public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityAdded)) : + new AuditEntry(); + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityAdded; // CHECK if the key should be resolved in POST Action #if EF5 || EF6 @@ -41,7 +45,7 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) { entry.DelayedKey = objectStateEntry; } - AuditEntityAdded(entry, objectStateEntry.CurrentValues); + AuditEntityAdded(entry, objectStateEntry, objectStateEntry.CurrentValues); #elif EFCORE // TODO: We must check if the key IsTemporary! We can maybe use flag... //if (!objectStateEntry.IsKeySet) @@ -57,9 +61,10 @@ public static void AuditEntityAdded(Audit audit, EntityEntry objectStateEntry) #if EF5 || EF6 /// Audit entity added. /// The audit entry. + /// The object state entry. /// The record. /// The prefix. - public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord record, string prefix = "") + public static void AuditEntityAdded(AuditEntry auditEntry, ObjectStateEntry objectStateEntry, DbUpdatableDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -70,11 +75,16 @@ public static void AuditEntityAdded(AuditEntry auditEntry, DbUpdatableDataRecord if (valueRecord != null) { // Complex Type - AuditEntityAdded(auditEntry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityAdded(auditEntry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) + else if (objectStateEntry.EntitySet.ElementType.KeyMembers.Any(x => x.Name == name) || auditEntry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(auditEntry.Entry, name)) { - auditEntry.Properties.Add(new AuditEntryProperty(auditEntry, string.Concat(prefix, name), null, value)); + var auditEntryProperty = auditEntry.Parent.Configuration.AuditEntryPropertyFactory != null ? + auditEntry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(auditEntry, objectStateEntry, string.Concat(prefix, name), null, value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(auditEntry, string.Concat(prefix, name), null, value); + auditEntry.Properties.Add(auditEntryProperty); } } } @@ -87,9 +97,14 @@ public static void AuditEntityAdded(AuditEntry entry, EntityEntry objectStateEnt { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, null, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, null, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, null, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityDeleted.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityDeleted.cs index 4505955f..845177b1 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityDeleted.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityDeleted.cs @@ -6,6 +6,8 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; +using System.Linq; + #if EF5 using System.Data.Objects; @@ -13,6 +15,7 @@ using System.Data.Entity.Core.Objects; #elif EFCORE +using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; #endif @@ -30,14 +33,16 @@ public static void AuditEntityDeleted(Audit audit, ObjectStateEntry objectStateE public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.EntityDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.EntityDeleted)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.EntityDeleted; #if EF5 || EF6 - AuditEntityDeleted(entry, objectStateEntry.OriginalValues); + AuditEntityDeleted(entry, objectStateEntry, objectStateEntry.OriginalValues); #elif EFCORE AuditEntityDeleted(entry, objectStateEntry); #endif @@ -49,7 +54,7 @@ public static void AuditEntityDeleted(Audit audit, EntityEntry objectStateEntry) /// The entry. /// The record. /// The prefix. - public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, string prefix = "") + public static void AuditEntityDeleted(AuditEntry entry, ObjectStateEntry objectStateEntry, DbDataRecord record, string prefix = "") { for (var i = 0; i < record.FieldCount; i++) { @@ -60,11 +65,16 @@ public static void AuditEntityDeleted(AuditEntry entry, DbDataRecord record, str if (valueRecord != null) { // Complex Type - AuditEntityDeleted(entry, valueRecord, string.Concat(prefix, name, ".")); + AuditEntityDeleted(entry, objectStateEntry, valueRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), value, null); + entry.Properties.Add(auditEntryProperty); } } } @@ -77,9 +87,14 @@ public static void AuditEntityDeleted(AuditEntry entry, EntityEntry objectStateE { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, null)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, null); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityModified.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityModified.cs index cfc256c0..af5db628 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityModified.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditEntityModified.cs @@ -32,10 +32,12 @@ public static void AuditEntityModified(Audit audit, ObjectStateEntry objectState public static void AuditEntityModified(Audit audit, EntityEntry objectStateEntry, AuditEntryState state) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = state - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, state)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = state; #if EF5 || EF6 AuditEntityModified(audit, entry, objectStateEntry, objectStateEntry.OriginalValues, objectStateEntry.CurrentValues); @@ -68,13 +70,19 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, ObjectStat AuditEntityModified(audit, entry, objectStateEntry, valueRecord, currentValue as DbUpdatableDataRecord, string.Concat(prefix, name, ".")); } - else if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) + else if (objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) + || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, name)) { if (!audit.Configuration.IgnorePropertyUnchanged || objectStateEntry.EntityKey.EntityKeyValues.Any(x => x.Key == name) || !Equals(currentValue, originalValue)) { - entry.Properties.Add(new AuditEntryProperty(entry, string.Concat(prefix, name), originalValue, currentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, string.Concat(prefix, name), originalValue, currentValue)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, string.Concat(prefix, name), originalValue, currentValue); + entry.Properties.Add(auditEntryProperty); } } } @@ -88,11 +96,16 @@ public static void AuditEntityModified(Audit audit, AuditEntry entry, EntityEntr { var property = objectStateEntry.Property(propertyEntry.Name); - if (entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) + if (property.Metadata.IsKey() || entry.Parent.CurrentOrDefaultConfiguration.IsAuditedProperty(entry.Entry, propertyEntry.Name)) { if (!audit.Configuration.IgnorePropertyUnchanged || property.Metadata.IsKey() || property.IsModified) { - entry.Properties.Add(new AuditEntryProperty(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? + entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, propertyEntry.Name, property.OriginalValue, property.CurrentValue)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, propertyEntry.Name, property.OriginalValue, property.CurrentValue); + entry.Properties.Add(auditEntryProperty); } } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipAdded.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipAdded.cs index 63f78fd3..7351d739 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipAdded.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipAdded.cs @@ -33,10 +33,12 @@ public static void AuditRelationAdded(Audit audit, ObjectStateEntry objectStateE public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipAdded - }; + var entry = audit.Configuration.AuditEntryFactory != null ? + audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipAdded)) : + new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipAdded; var values = objectStateEntry.CurrentValues; @@ -55,12 +57,22 @@ public static void AuditRelationAdded(Audit audit, EntityEntry objectStateEntry) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipDeleted.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipDeleted.cs index ec5b633a..fa31bedd 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipDeleted.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/AuditRelationshipDeleted.cs @@ -32,10 +32,12 @@ public static void AuditRelationDeleted(Audit audit, ObjectStateEntry objectStat public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntry) #endif { - var entry = new AuditEntry(audit, objectStateEntry) - { - State = AuditEntryState.RelationshipDeleted - }; + var entry = audit.Configuration.AuditEntryFactory != null ? +audit.Configuration.AuditEntryFactory(new AuditEntryFactoryArgs(audit, objectStateEntry, AuditEntryState.RelationshipDeleted)) : +new AuditEntry(); + + entry.Build(audit, objectStateEntry); + entry.State = AuditEntryState.RelationshipDeleted; var values = objectStateEntry.OriginalValues; for (var i = 0; i < values.FieldCount; i++) @@ -44,7 +46,12 @@ public static void AuditRelationDeleted(Audit audit, EntityEntry objectStateEntr var value = (EntityKey) values.GetValue(i); foreach (var keyValue in value.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, relationName, keyValue.Key, keyValue.Value, null)); + var auditEntryProperty = entry.Parent.Configuration.AuditEntryPropertyFactory != null ? +entry.Parent.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, relationName, keyValue.Key, keyValue.Value, null)) : +new AuditEntryProperty(); + + auditEntryProperty.Build(entry, relationName, keyValue.Key, keyValue.Value, null); + entry.Properties.Add(auditEntryProperty); } } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/PostSaveChanges.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/PostSaveChanges.cs index 6ae5cf98..99fe1cdb 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/PostSaveChanges.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/Audit/PostSaveChanges.cs @@ -49,19 +49,29 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in leftKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, leftRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, leftRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, leftRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } foreach (var keyValue in rightKeys.EntityKeyValues) { - entry.Properties.Add(new AuditEntryProperty(entry, rightRelationName, keyValue.Key, null, keyValue.Value)); + var auditEntryProperty = audit.Configuration.AuditEntryPropertyFactory != null ? + audit.Configuration.AuditEntryPropertyFactory(new AuditEntryPropertyArgs(entry, objectStateEntry, rightRelationName, keyValue.Key, null, keyValue.Value)) : + new AuditEntryProperty(); + + auditEntryProperty.Build(entry, rightRelationName, keyValue.Key, null, keyValue.Value); + entry.Properties.Add(auditEntryProperty); } } else { foreach (var keyValue in objectStateEntry.EntityKey.EntityKeyValues) { - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Key); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Key); // ENSURE the property is audited if (property != null) @@ -75,7 +85,7 @@ public static void PostSaveChanges(Audit audit) foreach (var keyValue in objectStateEntry.Metadata.GetKeys()) { var key = objectStateEntry.Property(keyValue.Properties[0].Name); - var property = entry.Properties.FirstOrDefault(x => x.PropertyName == keyValue.Properties[0].Name); + var property = entry.Properties.FirstOrDefault(x => x.InternalPropertyName == keyValue.Properties[0].Name); // ENSURE the property is audited if (property != null) diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditConfiguration.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditConfiguration.cs index a4271895..5614cb40 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditConfiguration.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditConfiguration.cs @@ -8,8 +8,14 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -#if EF5 || EF6 + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Core.Objects; #elif EFCORE using Microsoft.EntityFrameworkCore; @@ -38,6 +44,10 @@ public AuditConfiguration() ValueFormatterDictionary = new ConcurrentDictionary>(); } + public Func AuditEntryFactory { get; set; } + + public Func AuditEntryPropertyFactory { get; set; } + /// Gets or sets the automatic audit save pre action. /// The automatic audit save pre action. public Action AutoSavePreAction { get; set; } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntry.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntry.cs index 00301715..babc1094 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntry.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntry.cs @@ -25,35 +25,59 @@ namespace Z.EntityFramework.Plus /// An audit entry. public class AuditEntry { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntry() +#if EF5 || EF6 + public void Build(Audit parent, ObjectStateEntry entry) +#else + public void Build(Audit parent, EntityEntry entry) +#endif { - } + if (CreatedBy == null) + { + CreatedBy = parent.CreatedBy; + } + + if (CreatedDate == DateTime.MinValue) + { + CreatedDate = DateTime.Now; + } + + if (Entry == null) + { + Entry = entry; + } + + if (Parent == null) + { + Parent = parent; + } + + if (Properties == null) + { + Properties = new List(); + } - /// Constructor. - /// The audit parent. - /// The object state entry. #if EF5 || EF6 - public AuditEntry(Audit parent, ObjectStateEntry entry) -#elif EFCORE - public AuditEntry(Audit parent, EntityEntry entry) + if (EntitySetName == null) + { + EntitySetName = entry.EntitySet.Name; + } #endif - { - CreatedBy = parent.CreatedBy; - CreatedDate = DateTime.Now; - Entry = entry; - Parent = parent; - Properties = new List(); #if EF5 || EF6 - EntitySetName = entry.EntitySet.Name; - if (!entry.IsRelationship) + + if (EntityTypeName == null) { - EntityTypeName = entry.Entity.GetType().Name; + if (!entry.IsRelationship) + { + EntityTypeName = ObjectContext.GetObjectType(entry.Entity.GetType()).Name; + } } + #elif EFCORE - EntityTypeName = Entry.Entity.GetType().Name; + if (EntityTypeName == null) + { + EntityTypeName = Entry.Entity.GetType().Name; + } #endif } diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryFactoryArgs.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryFactoryArgs.cs new file mode 100644 index 00000000..26f5d5db --- /dev/null +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryFactoryArgs.cs @@ -0,0 +1,41 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryFactoryArgs + { +#if EF5 || EF6 + public AuditEntryFactoryArgs(Audit audit, ObjectStateEntry objectStateEntry, AuditEntryState auditEntryState) +#elif EFCORE + public AuditEntryFactoryArgs(Audit audit, EntityEntry entityEntry, AuditEntryState auditEntryState) +#endif + { + Audit = audit; + AuditEntryState = auditEntryState; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + + public Audit Audit { get; set; } + public AuditEntryState AuditEntryState { get; set; } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryProperty.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryProperty.cs index 60134f9f..7de7b9dc 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryProperty.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryProperty.cs @@ -13,38 +13,40 @@ namespace Z.EntityFramework.Plus /// An audit entry property. public class AuditEntryProperty { - /// Default constructor. - /// Required by Entity Framework. - public AuditEntryProperty() - { - } - /// Constructor. /// The audit entry parent. /// Name of the property audited. /// The old value audited. /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; + Build(parent, null, propertyName, oldValue, newValue); } - /// Constructor. - /// The audit entry parent. - /// The name of the relation audited. - /// Name of the property audited. - /// The old value audited. - /// The new value audited. - public AuditEntryProperty(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) + public void Build(AuditEntry parent, string relationName, string propertyName, object oldValue, object newValue) { - NewValue = newValue; - OldValue = oldValue; - Parent = parent; - PropertyName = propertyName; - RelationName = relationName; + InternalPropertyName = propertyName; + + if (!IsValueSet) + { + NewValue = newValue; + OldValue = oldValue; + } + + if (Parent == null) + { + Parent = parent; + } + + if (PropertyName == null) + { + PropertyName = propertyName; + } + + if (RelationName == null) + { + RelationName = relationName; + } } /// Gets or sets the identifier of the audit entry property. @@ -82,6 +84,15 @@ public AuditEntryProperty(AuditEntry parent, string relationName, string propert // EFCORE still have some issue with "NotMapped" attribute public object NewValue; #endif + /// Gets or sets a value indicating whether OldValue and NewValue is set. + /// true if OldValue and NewValue is set, false if not. + [NotMapped] + public bool IsValueSet { get; set; } + + /// Gets or sets the name of the property internally. + /// The name of the property internally. + [NotMapped] + public string InternalPropertyName { get; set; } /// Gets or sets the new value audited formatted. /// The new value audited formatted. diff --git a/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryPropertyArgs.cs b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryPropertyArgs.cs new file mode 100644 index 00000000..fe8ac325 --- /dev/null +++ b/src/Z.EntityFramework.Plus.EFCore/Audit/AuditEntryPropertyArgs.cs @@ -0,0 +1,56 @@ +#if EF5 +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.EntityFrameworkCore.ChangeTracking; + +#endif + +namespace Z.EntityFramework.Plus +{ + public class AuditEntryPropertyArgs + { +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string propertyName, object oldValue, object newValue) + : this(parent, objectStateEntry, null, propertyName, oldValue, newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string propertyName, object oldValue, object newValue) + : this(parent, entityEntry, null, propertyName, oldValue, newValue) +#endif + { + } + +#if EF5 || EF6 + public AuditEntryPropertyArgs(AuditEntry parent, ObjectStateEntry objectStateEntry, string relationName, string propertyName, object oldValue, object newValue) +#elif EFCORE + public AuditEntryPropertyArgs(AuditEntry parent, EntityEntry entityEntry, string relationName, string propertyName, object oldValue, object newValue) +#endif + { + AuditEntry = parent; + NewValue = newValue; + OldValue = oldValue; + PropertyName = propertyName; + RelationName = relationName; + +#if EF5 || EF6 + ObjectStateEntry = objectStateEntry; +#elif EFCORE + EntityEntry = entityEntry; +#endif + } + +#if EF5 || EF6 + public ObjectStateEntry ObjectStateEntry { get; set; } +#elif EFCORE + public EntityEntry EntityEntry { get; set; } +#endif + public AuditEntry AuditEntry { get; set; } + public string RelationName { get; set; } + public string PropertyName { get; set; } + public object OldValue { get; set; } + public object NewValue { get; set; } + } +} \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EFCore/Properties/AssemblyInfo.cs b/src/Z.EntityFramework.Plus.EFCore/Properties/AssemblyInfo.cs index 85301c34..29944499 100644 --- a/src/Z.EntityFramework.Plus.EFCore/Properties/AssemblyInfo.cs +++ b/src/Z.EntityFramework.Plus.EFCore/Properties/AssemblyInfo.cs @@ -18,5 +18,5 @@ [assembly: AssemblyCulture("")] [assembly: ComVisible(false)] [assembly: Guid("c41195f5-937e-4f2b-9f36-20650a804caa")] -[assembly: AssemblyVersion("1.3.12")] -[assembly: AssemblyFileVersion("1.3.12")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0")] +[assembly: AssemblyFileVersion("1.4.0")] \ No newline at end of file diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs index d2220296..09074591 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/DbContext/DbContext.GetObjectContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL +#if FULL || QUERY_FILTER #if EF5 || EF6 #if EF5 diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs index c3c116a7..e67d316b 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable/IQueryable.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE +#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_CACHE || QUERY_FILTER #if EF5 || EF6 using System; using System.Data.Entity; diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs index 1b1b7f8e..b211fb7f 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE #if EF5 || EF6 using System.Data.Entity; using System.Linq; diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs index b9b045e5..88f1426b 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF5_EF6/IQueryable`/IQueryable`.GetObjectQuery.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_DEFERRED || QUERY_FILTER || QUERY_FUTURE || QUERY_INCLUDEOPTIMIZED #if EF5 || EF6 using System; using System.Data.Entity.Infrastructure; @@ -41,17 +41,33 @@ internal static ObjectQuery GetObjectQuery(this IQueryable query) if (dbQuery == null) { - throw new Exception(ExceptionMessage.GeneralException); + var internalQueryProperty = query.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + + if (internalQueryProperty == null) + { + throw new Exception(ExceptionMessage.GeneralException); + } + + var internalQuery = internalQueryProperty.GetValue(query, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; + + return objectQuery; } - var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); - var internalQuery = internalQueryProperty.GetValue(dbQuery, null); - var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); - var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + { + var internalQueryProperty = dbQuery.GetType().GetProperty("InternalQuery", BindingFlags.NonPublic | BindingFlags.Instance); + var internalQuery = internalQueryProperty.GetValue(dbQuery, null); + var objectQueryContextProperty = internalQuery.GetType().GetProperty("ObjectQuery", BindingFlags.Public | BindingFlags.Instance); + var objectQueryContext = objectQueryContextProperty.GetValue(internalQuery, null); + + objectQuery = objectQueryContext as ObjectQuery; - objectQuery = objectQueryContext as ObjectQuery; + return objectQuery; + } - return objectQuery; } } } diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF6/ObjectContext/GetDbContext.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF6/ObjectContext/GetDbContext.cs index f1897025..4991c702 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EF6/ObjectContext/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EF6/ObjectContext/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FILTER #if EF6 using System.Data.Entity; using System.Data.Entity.Core.Objects; diff --git a/src/Z.EntityFramework.Plus.EFCore/_Internal/EFCore/IQueryable`/GetDbContext.cs b/src/Z.EntityFramework.Plus.EFCore/_Internal/EFCore/IQueryable`/GetDbContext.cs index 615b2f2c..4eb487d0 100644 --- a/src/Z.EntityFramework.Plus.EFCore/_Internal/EFCore/IQueryable`/GetDbContext.cs +++ b/src/Z.EntityFramework.Plus.EFCore/_Internal/EFCore/IQueryable`/GetDbContext.cs @@ -5,7 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. -#if FULL || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE +#if FULL || AUDIT || BATCH_DELETE || BATCH_UPDATE || QUERY_FUTURE #if EFCORE using System.Linq; using System.Reflection; diff --git a/src/Z.EntityFramework.Plus.EFCore/project.json b/src/Z.EntityFramework.Plus.EFCore/project.json index b1239e44..d80caaf7 100644 --- a/src/Z.EntityFramework.Plus.EFCore/project.json +++ b/src/Z.EntityFramework.Plus.EFCore/project.json @@ -1,5 +1,5 @@ { - "version": "1.3.12", + "version": "1.4.0", "description": "EF Bulk Operations & Utilities | Bulk Insert, Update, Delete, Merge from database.", "authors": [ "ZZZ Projects Inc." ], "packOptions": { diff --git a/src/Z.EntityFramework.Plus.sln b/src/Z.EntityFramework.Plus.sln index 16577858..c740c122 100644 --- a/src/Z.EntityFramework.Plus.sln +++ b/src/Z.EntityFramework.Plus.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.23107.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.EntityFramework.Plus.EF5", "Z.EntityFramework.Plus.EF5\Z.EntityFramework.Plus.EF5.csproj", "{1B28EFB7-E679-4275-ACA9-DA7CD589A56A}" EndProject @@ -52,7 +52,6 @@ Global {063FA382-DFDA-4BCB-BBB6-AAC04D0ED88E}.Release|Any CPU.ActiveCfg = Release|Any CPU {063FA382-DFDA-4BCB-BBB6-AAC04D0ED88E}.Release|Any CPU.Build.0 = Release|Any CPU {C47F920B-8064-44B0-BF3F-CD29298597A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C47F920B-8064-44B0-BF3F-CD29298597A0}.Debug|Any CPU.Build.0 = Debug|Any CPU {C47F920B-8064-44B0-BF3F-CD29298597A0}.Release|Any CPU.ActiveCfg = Release|Any CPU {C47F920B-8064-44B0-BF3F-CD29298597A0}.Release|Any CPU.Build.0 = Release|Any CPU {AE726FF5-89DE-42BE-B75D-834A7C1EB6E4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryFactory.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryFactory.cs new file mode 100644 index 00000000..22cb62c9 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryFactory.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_Configuration_AuditEntryFactory + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs new file mode 100644 index 00000000..bf1f34e8 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_CreatedBy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_CreatedBy.cs new file mode 100644 index 00000000..2f72bdb6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Audit_CreatedBy.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_CreatedBy + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityAdded.cs new file mode 100644 index 00000000..dd48c03e --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityAdded.cs @@ -0,0 +1,179 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs new file mode 100644 index 00000000..f585f168 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Entity_Basics); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityModified.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityModified.cs new file mode 100644 index 00000000..edec6f4f --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntityModified.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityModified() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs new file mode 100644 index 00000000..7b0dc8ce --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntitySoftAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs new file mode 100644 index 00000000..100497b9 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntitySoftDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs new file mode 100644 index 00000000..f193f2eb --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs @@ -0,0 +1,176 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void RelationshipAdded() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right { ColumnInt = 0 }; + var right1 = new Association_OneToMany_Right { ColumnInt = 1 }; + + left.Rights = new List { right0, right1 }; + + ctx.SaveChanges(audit); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs new file mode 100644 index 00000000..decc73af --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void RelationshipDeleted() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right { ColumnInt = 0 }; + var right1 = new Association_OneToMany_Right { ColumnInt = 1 }; + + left.Rights = new List { right0, right1 }; + + ctx.SaveChanges(); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Association_OneToMany_Rights); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs new file mode 100644 index 00000000..26768fbd --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs new file mode 100644 index 00000000..5bf0acd4 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs @@ -0,0 +1,183 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Entity_Basics); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs new file mode 100644 index 00000000..bb7fba64 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs @@ -0,0 +1,183 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityModified() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs new file mode 100644 index 00000000..62f284c0 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs @@ -0,0 +1,184 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntitySoftAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs new file mode 100644 index 00000000..1e7d83a2 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs @@ -0,0 +1,184 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntitySoftDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs new file mode 100644 index 00000000..2a916f7a --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs @@ -0,0 +1,176 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void RelationshipAdded() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right {ColumnInt = 0}; + var right1 = new Association_OneToMany_Right {ColumnInt = 1}; + + left.Rights = new List {right0, right1}; + + ctx.SaveChanges(audit); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs new file mode 100644 index 00000000..9147e313 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void RelationshipDeleted() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right {ColumnInt = 0}; + var right1 = new Association_OneToMany_Right {ColumnInt = 1}; + + left.Rights = new List {right0, right1}; + + ctx.SaveChanges(); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Association_OneToMany_Rights); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/All.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/All.cs index bf379a54..874d59bc 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/All.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/All.cs @@ -68,10 +68,24 @@ public void All() // Properties { + var propertyIndex = -1; + // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); - Assert.AreEqual(0, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); + Assert.AreEqual(1, entries[1].Properties.Count); + Assert.AreEqual(1, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); } } @@ -106,10 +120,24 @@ public void All() // Properties { + var propertyIndex = -1; + // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); - Assert.AreEqual(0, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); + Assert.AreEqual(1, entries[1].Properties.Count); + Assert.AreEqual(1, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/Type.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/Type.cs index 8b0b25ae..eb603d77 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/Type.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_ExcludeProperty/Type.cs @@ -71,16 +71,19 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); @@ -129,9 +132,9 @@ public void Type() // Entries TypeName // NOTE: Careful here, the order has changed since TPC_CAT do not insert property - Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[0].EntityTypeName); Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[1].EntityTypeName); - Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[2].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[2].EntityTypeName); } // Properties @@ -141,37 +144,40 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(3, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual((identitySeedDog + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual((identitySeedDog + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); // ColumnInt propertyIndex = 1; - Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); // ColumnCat | ColumnDog propertyIndex = 2; - Assert.AreEqual("ColumnDog", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnDog", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnDog", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Single.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Single.cs index d0d93825..2ee1172f 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Single.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Single.cs @@ -146,6 +146,7 @@ public void Single() Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); var s = (char) 13; + // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Type.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Type.cs index 4a70f463..1cfd887e 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Type.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/Configuration_IncludeProperty/Type.cs @@ -72,16 +72,19 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); @@ -130,9 +133,9 @@ public void Type() // Entries TypeName // NOTE: Careful here, the order has changed since TPC_CAT do not insert property - Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[0].EntityTypeName); Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[1].EntityTypeName); - Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[2].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[2].EntityTypeName); } // Properties @@ -142,37 +145,40 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(3, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual((identitySeedDog + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual((identitySeedDog + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); // ColumnInt propertyIndex = 1; - Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); // ColumnCat | ColumnDog propertyIndex = 2; - Assert.AreEqual("ColumnDog", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnDog", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnDog", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/CreatedBy/CurrentPrincipal.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/CreatedBy/CurrentPrincipal.cs new file mode 100644 index 00000000..7514fc2c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/CreatedBy/CurrentPrincipal.cs @@ -0,0 +1,179 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_CreatedBy + { + [TestMethod] + public void CurrentPrincipal() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof(Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[2].EntityTypeName); + + // Entries CreatedBy + Assert.AreEqual("XPS8700\\Jonathan", entries[0].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[1].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[2].CreatedBy); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof(Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[2].EntityTypeName); + + // Entries CreatedBy + Assert.AreEqual("XPS8700\\Jonathan", entries[0].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[1].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[2].CreatedBy); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityAdded/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityAdded/Entity_Proxy.cs new file mode 100644 index 00000000..16a5a19e --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityAdded/Entity_Proxy.cs @@ -0,0 +1,169 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityAdded + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Proxies, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityDeleted/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityDeleted/Entity_Proxy.cs new file mode 100644 index 00000000..8f1e43fc --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityDeleted/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 + +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityDeleted + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + TestContext.DeleteAll(ctx, x => x.Entity_Proxies); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityModified/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityModified/Entity_Proxy.cs new file mode 100644 index 00000000..71e481e6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntityModified/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 + +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityModified + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftAdded/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftAdded/Entity_Proxy.cs new file mode 100644 index 00000000..399e6681 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftAdded/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntitySoftAdded + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftDeleted/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftDeleted/Entity_Proxy.cs new file mode 100644 index 00000000..540f6678 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Audit/EntitySoftDeleted/Entity_Proxy.cs @@ -0,0 +1,186 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntitySoftDeleted + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/Z.Test.EntityFramework.Plus.EF5.csproj b/src/test/Z.Test.EntityFramework.Plus.EF5/Z.Test.EntityFramework.Plus.EF5.csproj index 634aff14..3b5c1f73 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/Z.Test.EntityFramework.Plus.EF5.csproj +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/Z.Test.EntityFramework.Plus.EF5.csproj @@ -69,6 +69,8 @@ + + @@ -76,6 +78,7 @@ + @@ -85,6 +88,20 @@ + + + + + + + + + + + + + + @@ -119,28 +136,34 @@ + + + + + + @@ -399,6 +422,7 @@ + @@ -411,13 +435,19 @@ + + + + + + @@ -440,6 +470,7 @@ + diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/AuditHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/AuditHelper.cs index f0ba3f69..46c2f36c 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/AuditHelper.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/AuditHelper.cs @@ -5,6 +5,10 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; using Z.EntityFramework.Plus; namespace Z.Test.EntityFramework.Plus @@ -14,9 +18,48 @@ public static class AuditHelper public static Audit AutoSaveAudit() { var audit = new Audit(); -#if EF5 || EF6 audit.Configuration.AutoSavePreAction = (context, audit1) => (context as TestContext).AuditEntries.AddRange(audit1.Entries); -#endif + return audit; + } + + public static Audit AutoSaveWithAuditEntryFactory() + { + var audit = new Audit(); + audit.Configuration.AutoSavePreAction = (context, audit1) => + { + (context as TestContext).AuditEntry_Extendeds.AddRange(audit1.Entries.Cast()); + }; + + audit.Configuration.AuditEntryFactory = args => + { + return new AuditEntry_Extended + { + CreatedBy = "CustomCreatedBy", + CreatedDate = new DateTime(1981, 04, 13), + EntitySetName = "CustomEntitySetName", + EntityTypeName = "CustomEntityTypeName", + ExtendedValue = "CustomExtendedValue" + }; + }; + + return audit; + } + + public static Audit AutoSaveWithAuditEntryPropertyFactory() + { + var audit = new Audit(); + audit.CreatedBy = "ZZZ Projects"; + audit.Configuration.AutoSavePreAction = (context, audit1) => (context as TestContext).AuditEntries.AddRange(audit1.Entries); + + audit.Configuration.AuditEntryPropertyFactory = args => + { + return new AuditEntryProperty_Extended + { + RelationName = "CustomRelationName", + PropertyName = "CustomPropertyName", + ExtendedValue = "CustomExtendedValue" + }; + }; return audit; } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/Extensions/QueryCacheHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/Extensions/QueryCacheHelper.cs new file mode 100644 index 00000000..794adc08 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/Extensions/QueryCacheHelper.cs @@ -0,0 +1,26 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + + +#if EFCORE +using Microsoft.Data.Entity; +using Microsoft.Extensions.Caching.Memory; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public static class TestExtensions + { +#if EFCORE + public static DbContext GetObjectContext(this DbContext context) + { + return context; + } +#endif + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryCacheHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryCacheHelper.cs index 7de2ccc3..df59d844 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryCacheHelper.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryCacheHelper.cs @@ -7,7 +7,7 @@ using Z.EntityFramework.Plus; -#if EF7 +#if EFCORE using Microsoft.Extensions.Caching.Memory; #endif @@ -20,7 +20,7 @@ public static long GetCacheCount() { #if EF5 || EF6 return QueryCacheManager.Cache.GetCount(); -#elif EF7 +#elif EFCORE return ((MemoryCache) QueryCacheManager.Cache).Count; #endif } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryFilterHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryFilterHelper.cs index 14250b4f..5aaa369d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryFilterHelper.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Helper/QueryFilterHelper.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System; using System.Linq; using Z.EntityFramework.Plus; @@ -24,14 +25,27 @@ public enum Filter Filter8 } + public static object SingleThreadLock = new Object(); public static void ClearGlobalManagerFilter() { +#if EF5 QueryFilterManager.GlobalFilters.Clear(); QueryFilterManager.GlobalInitializeFilterActions.Clear(); +#elif EF6 + QueryFilterManager.GlobalFiltersByKey.Clear(); + QueryFilterManager.GlobalFilterByType.Clear(); +#endif + } public static void CreateGlobalManagerFilter(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) { + ClearGlobalManagerFilter(); +#if EFCORE + // TODO: Remove this when cast issue will be fixed + QueryFilterManager.GlobalFilters.Clear(); + QueryFilterManager.GlobalInitializeFilterActions.Clear(); +#endif if (enableFilter1 != null) { QueryFilterManager.Filter(Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), isEnabled); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntryProperty_Extended.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntryProperty_Extended.cs new file mode 100644 index 00000000..8c7f7fc0 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntryProperty_Extended.cs @@ -0,0 +1,9 @@ +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public class AuditEntryProperty_Extended : AuditEntryProperty + { + public string ExtendedValue { get; set; } + } +} diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntry_Extended.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntry_Extended.cs new file mode 100644 index 00000000..a5f84a8c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/AuditEntry_Extended.cs @@ -0,0 +1,9 @@ +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public class AuditEntry_Extended : AuditEntry + { + public string ExtendedValue { get; set; } + } +} diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Enum.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Enum.cs new file mode 100644 index 00000000..13c67c45 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Enum.cs @@ -0,0 +1,20 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System; + +namespace Z.Test.EntityFramework.Plus +{ + public class Entity_Enum + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public DayOfWeek Day { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy.cs new file mode 100644 index 00000000..7b4011a5 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Z.Test.EntityFramework.Plus +{ + public class Entity_Proxy + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public virtual ICollection Rights { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy_Right.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy_Right.cs new file mode 100644 index 00000000..4ff754e6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Entity_Proxy_Right.cs @@ -0,0 +1,11 @@ +namespace Z.Test.EntityFramework.Plus +{ + public class Entity_Proxy_Right + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public Entity_Proxy Left { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Inheritance_Interface_Entity_LazyLoading.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Inheritance_Interface_Entity_LazyLoading.cs new file mode 100644 index 00000000..ea4b8437 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Inheritance_Interface_Entity_LazyLoading.cs @@ -0,0 +1,20 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; + +namespace Z.Test.EntityFramework.Plus +{ + public class Inheritance_Interface_Entity_LazyLoading + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public virtual Collection Rights { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic.cs index cacd7bc7..b715f2ba 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if EF5 || EF6 using System.Data.Entity.ModelConfiguration; namespace Z.Test.EntityFramework.Plus @@ -23,4 +24,5 @@ public EntityPropertyVisibilityConfiguration() } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic_Many.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic_Many.cs index 4595e314..17fe9c4d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic_Many.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Internal_Entity_Basic_Many.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +#if EF5 || EF6 using System.Data.Entity.ModelConfiguration; namespace Z.Test.EntityFramework.Plus @@ -29,4 +30,5 @@ public EntityPropertyVisibilityConfiguration() } } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/DeleteAll.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/DeleteAll.cs index 5f7bc179..6c9c531e 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/DeleteAll.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/DeleteAll.cs @@ -15,7 +15,7 @@ #elif EF6 using System.Data.Entity; -#elif EF7 +#elif EFCORE using Microsoft.Data.Entity; #endif diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/GetIdentitySeed.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/GetIdentitySeed.cs index 0cac8391..4ee52d8a 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/GetIdentitySeed.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/GetIdentitySeed.cs @@ -10,7 +10,7 @@ #if EF5 || EF6 using System.Data.Entity; -#elif EF7 +#elif EFCORE using Microsoft.Data.Entity; #endif diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/Insert.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/Insert.cs index 87fa29b5..ae99e956 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/Insert.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/Insert.cs @@ -17,7 +17,7 @@ #elif EF6 using System.Data.Entity; -#elif EF7 +#elif EFCORE using Microsoft.Data.Entity; #endif diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/TypeName.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/TypeName.cs index 5446f6a8..8aa89600 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/TypeName.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/Methods/TypeName.cs @@ -11,7 +11,7 @@ #if EF5 || EF6 using System.Data.Entity; -#elif EF7 +#elif EFCORE using Microsoft.Data.Entity; #endif diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContext.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContext.cs index 9849dfd8..a67834e2 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContext.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContext.cs @@ -5,13 +5,18 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System.Data.Common; using System.Data.SqlClient; using System.Linq; using Z.EntityFramework.Plus; -#if EF5 || EF6 +#if EF5 +using System.Data.Entity; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Infrastructure.Interception; -#elif EF7 +#elif EFCORE using System.Configuration; using System.Data.SqlClient; using Microsoft.Data.Entity; @@ -20,11 +25,13 @@ namespace Z.Test.EntityFramework.Plus { + + +#if EF5 || EF6 public class TestContextInitializer : CreateDatabaseIfNotExists { protected override void Seed(TestContext context) { -#if EF5 || EF6 var sql = @" TRUNCATE TABLE Inheritance_TPC_Cat IF IDENT_CURRENT( 'Inheritance_TPC_Cat' ) < 1000000 @@ -38,45 +45,56 @@ DBCC CHECKIDENT('Inheritance_TPC_Cat', RESEED, 1000000) connection.Open(); command.ExecuteNonQuery(); } - -#endif base.Seed(context); } } +#endif public partial class TestContext : DbContext { #if EF5 || EF6 public TestContext() : base(My.Config.ConnectionStrings.TestDatabase) -#elif EF7 +#elif EFCORE public TestContext() #endif { #if EF5 || EF6 Database.SetInitializer(new TestContextInitializer()); -#elif EF7 +#elif EFCORE Database.EnsureCreated(); #endif + +#if EF6 + // BE careful, this one also need to clear filter! + QueryFilterManager.ClearQueryCache(this); +#endif + } #if EF5 || EF6 public TestContext(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) : base(My.Config.ConnectionStrings.TestDatabase) -#elif EF7 +#elif EFCORE public TestContext(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) #endif { #if EF5 || EF6 Database.SetInitializer(new CreateDatabaseIfNotExists()); -#elif EF7 +#elif EFCORE Database.EnsureCreated(); #endif -#if EF7 +#if EFCORE // TODO: Remove this when cast issue will be fixed QueryFilterManager.GlobalFilters.Clear(); QueryFilterManager.GlobalInitializeFilterActions.Clear(); #endif +#if EF6 + // Clear query cache + QueryFilterManager.ClearQueryCache(this); +#endif + + if (enableFilter1 != null) { this.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), isEnabled); @@ -244,7 +262,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder) modelBuilder.Configurations.Add(new Internal_Entity_Basic.EntityPropertyVisibilityConfiguration()); modelBuilder.Configurations.Add(new Internal_Entity_Basic_Many.EntityPropertyVisibilityConfiguration()); } -#elif EF7 +#elif EFCORE protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(new SqlConnection(My.Config.ConnectionStrings.TestDatabase)); @@ -253,6 +271,8 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity().HasKey(guid => new {guid.ID1, guid.ID2, guid.ID3}); + // Association { } @@ -306,13 +326,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) #region Audit -#if EF5 || EF6 - public DbSet AuditEntries { get; set; } public DbSet AuditEntryProperties { get; set; } -#endif + + public DbSet AuditEntry_Extendeds { get; set; } + + public DbSet AuditEntryProperty_Extendeds { get; set; } #endregion @@ -324,16 +345,25 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Entity_Basic_Manies { get; set; } +#if EF5 || EF6 public DbSet Internal_Entity_Basics { get; set; } public DbSet Internal_Entity_Basic_Manies { get; set; } +#endif + public DbSet Entity_Guids { get; set; } public DbSet Entity_ManyGuids { get; set; } + public DbSet Entity_Proxies { get; set; } + + public DbSet Entity_Proxy_Rights { get; set; } + #if EF5 || EF6 public DbSet Entity_Complexes { get; set; } + + public DbSet Entity_Enums { get; set; } #endif #endregion @@ -342,6 +372,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Inheritance_Interface_Entities { get; set; } + public DbSet Inheritance_Interface_Entities_LazyLoading { get; set; } + #if EF5 || EF6 public DbSet Inheritance_TPC_Animals { get; set; } @@ -354,6 +386,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Property_AllTypes { get; set; } - #endregion +#endregion } } \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContextMemory.cs b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContextMemory.cs new file mode 100644 index 00000000..63de54c6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF5/_Model/_TestContextMemory.cs @@ -0,0 +1,377 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EFCORE +using System.Data.SqlClient; +using System.Linq; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using System.Configuration; +using System.Data.SqlClient; +using Microsoft.EntityFrameworkCore; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ +#if EF5 || EF6 + public class TestContextInitializer : CreateDatabaseIfNotExists + { + protected override void Seed(TestContext context) + { + var sql = @" +TRUNCATE TABLE Inheritance_TPC_Cat +IF IDENT_CURRENT( 'Inheritance_TPC_Cat' ) < 1000000 +BEGIN + DBCC CHECKIDENT('Inheritance_TPC_Cat', RESEED, 1000000) +END +"; + using (var connection = new SqlConnection(My.Config.ConnectionStrings.TestDatabase)) + using (var command = new SqlCommand(sql, connection)) + { + connection.Open(); + command.ExecuteNonQuery(); + } + base.Seed(context); + } + } +#endif + + public partial class TestContextMemory : DbContext + { +#if EF5 || EF6 + public TestContext() : base(My.Config.ConnectionStrings.TestDatabase) +#elif EFCORE + public TestContextMemory() +#endif + { +#if EF5 || EF6 + Database.SetInitializer(new TestContextInitializer()); +#elif EFCORE + Database.EnsureCreated(); +#endif + } + +#if EFCORE + public TestContextMemory(DbContextOptions options) : base(options) + { + + } +#endif + + +#if EF5 || EF6 + public TestContext(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) : base(My.Config.ConnectionStrings.TestDatabase) +#elif EFCORE + public TestContextMemory(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) +#endif + { +#if EF5 || EF6 + Database.SetInitializer(new CreateDatabaseIfNotExists()); +#elif EFCORE + Database.EnsureCreated(); +#endif +#if EFCORE + // TODO: Remove this when cast issue will be fixed + QueryFilterManager.GlobalFilters.Clear(); + QueryFilterManager.GlobalInitializeFilterActions.Clear(); +#endif + + if (enableFilter1 != null) + { + this.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), isEnabled); + if (!isEnabled && enableFilter1.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter1).Enable(); + } + else if (isEnabled && !enableFilter1.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter1).Disable(); + } + } + if (enableFilter2 != null) + { + this.Filter(QueryFilterHelper.Filter.Filter2, entities => entities.Where(x => x.ColumnInt != 2), isEnabled); + if (!isEnabled && enableFilter2.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter2).Enable(); + } + else if (isEnabled && !enableFilter2.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter2).Disable(); + } + } + if (enableFilter3 != null) + { + this.Filter(QueryFilterHelper.Filter.Filter3, entities => entities.Where(x => x.ColumnInt != 3), isEnabled); + if (!isEnabled && enableFilter3.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter3).Enable(); + } + else if (isEnabled && !enableFilter3.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter3).Disable(); + } + } + if (enableFilter4 != null) + { + this.Filter(QueryFilterHelper.Filter.Filter4, entities => entities.Where(x => x.ColumnInt != 4), isEnabled); + if (!isEnabled && enableFilter4.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter4).Enable(); + } + else if (isEnabled && !enableFilter4.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter4).Disable(); + } + } + + if (excludeClass != null && excludeClass.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter1).Disable(typeof (Inheritance_Interface_Entity)); + } + + if (excludeInterface != null && excludeInterface.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter2).Disable(typeof (Inheritance_Interface_IEntity)); + } + + if (excludeBaseClass != null && excludeBaseClass.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter3).Disable(typeof (Inheritance_Interface_Base)); + } + + if (excludeBaseInterface != null && excludeBaseInterface.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter4).Disable(typeof (Inheritance_Interface_IBase)); + } + + if (includeClass != null && includeClass.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter1).Enable(typeof (Inheritance_Interface_IEntity)); + } + + if (includeInterface != null && includeInterface.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter2).Enable(typeof (Inheritance_Interface_IEntity)); + } + + if (includeBaseClass != null && includeBaseClass.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter3).Enable(typeof (Inheritance_Interface_Base)); + } + + if (includeBaseInterface != null && includeBaseInterface.Value) + { + this.Filter(QueryFilterHelper.Filter.Filter4).Enable(typeof (Inheritance_Interface_IBase)); + } + } + +#if EF5 || EF6 + protected override void OnModelCreating(DbModelBuilder modelBuilder) + { + // Association + { + // Many to Many + { + modelBuilder.Entity() + .HasMany(x => x.Rights) + .WithMany(x => x.Lefts) + .Map(c => + { + c.MapLeftKey("ChildID"); + c.MapRightKey("ParentID"); + c.ToTable("Association_ManyToMany"); + }); + } + + // Many + { + modelBuilder.Entity() + .HasMany(x => x.Rights) + .WithRequired(x => x.Left); + } + } + + // Association Multi + { + // Many + { + modelBuilder.Entity() + .HasMany(x => x.Right1s) + .WithRequired(x => x.Left); + + modelBuilder.Entity() + .HasMany(x => x.Right2s) + .WithRequired(x => x.Left); + + modelBuilder.Entity() + .HasMany(x => x.Rights) + .WithRequired(x => x.Left); + + modelBuilder.Entity() + .HasMany(x => x.Rights) + .WithRequired(x => x.Left); + } + } + + // Entity + { + modelBuilder.ComplexType(); + modelBuilder.ComplexType(); + } + + // Inheritance + { + modelBuilder.Entity().Map(m => + { + m.MapInheritedProperties(); + m.ToTable("Inheritance_TPC_Cat"); + }); + + modelBuilder.Entity().Map(m => + { + m.MapInheritedProperties(); + m.ToTable("Inheritance_TPC_Dog"); + }); + } + + // Many + { + modelBuilder.Entity().HasMany(x => x.Properties).WithRequired(x => x.Parent); + } + + modelBuilder.Configurations.Add(new Internal_Entity_Basic.EntityPropertyVisibilityConfiguration()); + modelBuilder.Configurations.Add(new Internal_Entity_Basic_Many.EntityPropertyVisibilityConfiguration()); + } +#elif EFCORE + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // optionsBuilder.UseSqlServer(new SqlConnection(My.Config.ConnectionStrings.TestDatabase)); + } + + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + foreach (var entity in modelBuilder.Model.GetEntityTypes()) + { + entity.Relational().TableName = entity.DisplayName(); + } + + AuditManager.DefaultConfiguration.AutoSavePreAction = (ApplicationDbContext, audit) => +(ApplicationDbContext as TestContext).AuditEntries.AddRange(audit.Entries); + + modelBuilder.Entity().HasKey(guid => new {guid.ID1, guid.ID2, guid.ID3}); + + // Association + { + } + + // Audit + { + //modelBuilder.Entity().HasKey(x => x.AuditEntryID); + //modelBuilder.Entity().Ignore(x => x.NewValue); + //modelBuilder.Entity().Ignore(x => x.OldValue); + //modelBuilder.Entity().HasMany(x => x.Properties).WithOne(x => x.AuditEntry); + //modelBuilder.Entity().HasKey(x => x.AuditEntryPropertyID); + + } + } + +#endif + + #region Association + +#if EF5 || EF6 + public DbSet Association_ManyToMany_Lefts { get; set; } + + public DbSet Association_ManyToMany_Rights { get; set; } + +#endif + public DbSet Association_OneToMany_Lefts { get; set; } + + public DbSet Association_OneToMany_Rights { get; set; } + + #endregion + + #region Association Multi + + public DbSet Association_Multi_OneToMany_Lefts { get; set; } + + public DbSet Association_Multi_OneToMany_Right1s { get; set; } + + public DbSet Association_Multi_OneToMany_Right2s { get; set; } + + #endregion + + #region Association ToManyToMany + + public DbSet Association_Multi_OneToManyToMany_Lefts { get; set; } + + public DbSet Association_Multi_OneToManyToMany_Rights { get; set; } + + public DbSet Association_Multi_OneToManyToMany_RightRights { get; set; } + + #endregion + + #region Audit + + public DbSet AuditEntries { get; set; } + + public DbSet AuditEntryProperties { get; set; } + + #endregion + + #region Entity + + public DbSet Entity_Basics { get; set; } + + public DbSet ZZZ_Entity_Basics { get; set; } + + public DbSet Entity_Basic_Manies { get; set; } + +#if EF5 || EF6 + public DbSet Internal_Entity_Basics { get; set; } + + public DbSet Internal_Entity_Basic_Manies { get; set; } +#endif + + + public DbSet Entity_Guids { get; set; } + + public DbSet Entity_ManyGuids { get; set; } + +#if EF5 || EF6 + public DbSet Entity_Complexes { get; set; } +#endif + +#endregion + +#region Inheritance + + public DbSet Inheritance_Interface_Entities { get; set; } + +#if EF5 || EF6 + public DbSet Inheritance_TPC_Animals { get; set; } + + public DbSet Inheritance_TPH_Animals { get; set; } + + public DbSet Inheritance_TPT_Animals { get; set; } + +#endif + public DbSet Inheritance_TPT_Cats { get; set; } + + public DbSet Property_AllTypes { get; set; } + +#endregion + } +} +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryFactory.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryFactory.cs new file mode 100644 index 00000000..22cb62c9 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryFactory.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_Configuration_AuditEntryFactory + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs new file mode 100644 index 00000000..bf1f34e8 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_Configuration_AuditEntryPropertyFactory.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_CreatedBy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_CreatedBy.cs new file mode 100644 index 00000000..2f72bdb6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Audit_CreatedBy.cs @@ -0,0 +1,16 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + [TestClass] + public partial class Audit_CreatedBy + { + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityAdded.cs new file mode 100644 index 00000000..dd48c03e --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityAdded.cs @@ -0,0 +1,179 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs new file mode 100644 index 00000000..f585f168 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityDeleted.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Entity_Basics); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityModified.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityModified.cs new file mode 100644 index 00000000..edec6f4f --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntityModified.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntityModified() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs new file mode 100644 index 00000000..7b0dc8ce --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftAdded.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntitySoftAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs new file mode 100644 index 00000000..100497b9 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/EntitySoftDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void EntitySoftDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[2].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[2].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs new file mode 100644 index 00000000..f193f2eb --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipAdded.cs @@ -0,0 +1,176 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void RelationshipAdded() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right { ColumnInt = 0 }; + var right1 = new Association_OneToMany_Right { ColumnInt = 1 }; + + left.Rights = new List { right0, right1 }; + + ctx.SaveChanges(audit); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs new file mode 100644 index 00000000..decc73af --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryFactory/RelationshipDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryFactory + { + [TestMethod] + public void RelationshipDeleted() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right { ColumnInt = 0 }; + var right1 = new Association_OneToMany_Right { ColumnInt = 1 }; + + left.Rights = new List { right0, right1 }; + + ctx.SaveChanges(); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Association_OneToMany_Rights); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries.Cast().ToList(); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntry_Extendeds.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("CustomEntitySetName", entries[0].EntitySetName); + Assert.AreEqual("CustomEntitySetName", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual("CustomEntityTypeName", entries[0].EntityTypeName); + Assert.AreEqual("CustomEntityTypeName", entries[1].EntityTypeName); + + // Entries ExtendedValue + Assert.AreEqual("CustomExtendedValue", entries[0].ExtendedValue); + Assert.AreEqual("CustomExtendedValue", entries[1].ExtendedValue); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Source", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("Association_OneToMany_Left_Rights_Target", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs new file mode 100644 index 00000000..26768fbd --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityAdded.cs @@ -0,0 +1,181 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs new file mode 100644 index 00000000..5bf0acd4 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityDeleted.cs @@ -0,0 +1,183 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Entity_Basics); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs new file mode 100644 index 00000000..bb7fba64 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntityModified.cs @@ -0,0 +1,183 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntityModified() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs new file mode 100644 index 00000000..62f284c0 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftAdded.cs @@ -0,0 +1,184 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntitySoftAdded() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs new file mode 100644 index 00000000..1e7d83a2 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/EntitySoftDeleted.cs @@ -0,0 +1,184 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void EntitySoftDeleted() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + TestContext.Insert(x => x.Entity_Basics, 3); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + ctx.Entity_Basics.ToList().ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Basic).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[2].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs new file mode 100644 index 00000000..2a916f7a --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipAdded.cs @@ -0,0 +1,176 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void RelationshipAdded() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right {ColumnInt = 0}; + var right1 = new Association_OneToMany_Right {ColumnInt = 1}; + + left.Rights = new List {right0, right1}; + + ctx.SaveChanges(audit); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(5, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipAdded, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs new file mode 100644 index 00000000..9147e313 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_AuditEntryPropertyFactory/RelationshipDeleted.cs @@ -0,0 +1,182 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_Configuration_AuditEntryPropertyFactory + { + [TestMethod] + public void RelationshipDeleted() + { + int leftID; + int rightID_0; + int rightID_1; + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Association_OneToMany_Lefts); + TestContext.DeleteAll(x => x.Association_OneToMany_Rights); + + var audit = AuditHelper.AutoSaveWithAuditEntryPropertyFactory(); + + using (var ctx = new TestContext()) + { + var left = TestContext.Insert(ctx, x => x.Association_OneToMany_Lefts, 1).First(); + var right0 = new Association_OneToMany_Right {ColumnInt = 0}; + var right1 = new Association_OneToMany_Right {ColumnInt = 1}; + + left.Rights = new List {right0, right1}; + + ctx.SaveChanges(); + + leftID = left.ID; + rightID_0 = right0.ID; + rightID_1 = right1.ID; + } + + using (var ctx = new TestContext()) + { + TestContext.DeleteAll(ctx, x => x.Association_OneToMany_Rights); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(4, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.RelationshipDeleted, entries[1].State); + + // Entries EntitySetName + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[0].EntitySetName); + Assert.AreEqual("Association_OneToMany_Left_Rights", entries[1].EntitySetName); + + // Entries TypeName + Assert.AreEqual(null, entries[0].EntityTypeName); + Assert.AreEqual(null, entries[1].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + + // Association_OneToMany_Left_Rights_Source;ID + propertyIndex = 0; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(leftID.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(leftID.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + + propertyIndex = 1; + Assert.AreEqual("CustomRelationName", entries[0].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomRelationName", entries[1].Properties[propertyIndex].RelationName); + Assert.AreEqual("CustomPropertyName", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("CustomPropertyName", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual(rightID_0.ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(rightID_1.ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[0].Properties[propertyIndex]).ExtendedValue); + Assert.AreEqual("CustomExtendedValue", ((AuditEntryProperty_Extended)entries[1].Properties[propertyIndex]).ExtendedValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/All.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/All.cs index bf379a54..874d59bc 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/All.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/All.cs @@ -68,10 +68,24 @@ public void All() // Properties { + var propertyIndex = -1; + // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); - Assert.AreEqual(0, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); + Assert.AreEqual(1, entries[1].Properties.Count); + Assert.AreEqual(1, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); } } @@ -106,10 +120,24 @@ public void All() // Properties { + var propertyIndex = -1; + // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); - Assert.AreEqual(0, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); + Assert.AreEqual(1, entries[1].Properties.Count); + Assert.AreEqual(1, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/Type.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/Type.cs index 8b0b25ae..eb603d77 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/Type.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_ExcludeProperty/Type.cs @@ -71,16 +71,19 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); @@ -129,9 +132,9 @@ public void Type() // Entries TypeName // NOTE: Careful here, the order has changed since TPC_CAT do not insert property - Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[0].EntityTypeName); Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[1].EntityTypeName); - Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[2].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[2].EntityTypeName); } // Properties @@ -141,37 +144,40 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(3, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual((identitySeedDog + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual((identitySeedDog + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); // ColumnInt propertyIndex = 1; - Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); // ColumnCat | ColumnDog propertyIndex = 2; - Assert.AreEqual("ColumnDog", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnDog", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnDog", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Single.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Single.cs index d0d93825..2ee1172f 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Single.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Single.cs @@ -146,6 +146,7 @@ public void Single() Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); var s = (char) 13; + // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Type.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Type.cs index 4a70f463..1cfd887e 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Type.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/Configuration_IncludeProperty/Type.cs @@ -72,16 +72,19 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(0, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeedCat + 1, entries[0].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 1, entries[1].Properties[propertyIndex].NewValue); Assert.AreEqual(identitySeedDog + 2, entries[2].Properties[propertyIndex].NewValue); @@ -130,9 +133,9 @@ public void Type() // Entries TypeName // NOTE: Careful here, the order has changed since TPC_CAT do not insert property - Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[0].EntityTypeName); Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[1].EntityTypeName); - Assert.AreEqual(typeof (Inheritance_TPC_Cat).Name, entries[2].EntityTypeName); + Assert.AreEqual(typeof (Inheritance_TPC_Dog).Name, entries[2].EntityTypeName); } // Properties @@ -142,37 +145,40 @@ public void Type() var propertyIndex = -1; // Properties Count - Assert.AreEqual(3, entries[0].Properties.Count); + Assert.AreEqual(1, entries[0].Properties.Count); Assert.AreEqual(3, entries[1].Properties.Count); - Assert.AreEqual(0, entries[2].Properties.Count); + Assert.AreEqual(3, entries[2].Properties.Count); // ID propertyIndex = 0; Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual((identitySeedDog + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual((identitySeedDog + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeedCat + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 1).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeedDog + 2).ToString(), entries[2].Properties[propertyIndex].NewValue); // ColumnInt propertyIndex = 1; - Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); // ColumnCat | ColumnDog propertyIndex = 2; - Assert.AreEqual("ColumnDog", entries[0].Properties[propertyIndex].PropertyName); Assert.AreEqual("ColumnDog", entries[1].Properties[propertyIndex].PropertyName); - Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("ColumnDog", entries[2].Properties[propertyIndex].PropertyName); Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); - Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); - Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[2].Properties[propertyIndex].NewValue); } } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/CreatedBy/CurrentPrincipal.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/CreatedBy/CurrentPrincipal.cs new file mode 100644 index 00000000..7514fc2c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/CreatedBy/CurrentPrincipal.cs @@ -0,0 +1,179 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_CreatedBy + { + [TestMethod] + public void CurrentPrincipal() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Basics); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Basics); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Basics, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof(Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[2].EntityTypeName); + + // Entries CreatedBy + Assert.AreEqual("XPS8700\\Jonathan", entries[0].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[1].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[2].CreatedBy); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Basics), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof(Entity_Basic).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof(Entity_Basic).Name, entries[2].EntityTypeName); + + // Entries CreatedBy + Assert.AreEqual("XPS8700\\Jonathan", entries[0].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[1].CreatedBy); + Assert.AreEqual("XPS8700\\Jonathan", entries[2].CreatedBy); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityAdded/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityAdded/Entity_Proxy.cs new file mode 100644 index 00000000..16a5a19e --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityAdded/Entity_Proxy.cs @@ -0,0 +1,169 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 || EF6 +using System.Data.Entity; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityAdded + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + TestContext.Insert(ctx, x => x.Entity_Proxies, 3); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityDeleted/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityDeleted/Entity_Proxy.cs new file mode 100644 index 00000000..8f1e43fc --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityDeleted/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 + +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityDeleted + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + TestContext.DeleteAll(ctx, x => x.Entity_Proxies); + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(null, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(null, entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityModified/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityModified/Entity_Proxy.cs new file mode 100644 index 00000000..71e481e6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntityModified/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 + +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntityModified + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntityModified, entries[0].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[1].State); + Assert.AreEqual(AuditEntryState.EntityModified, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftAdded/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftAdded/Entity_Proxy.cs new file mode 100644 index 00000000..399e6681 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftAdded/Entity_Proxy.cs @@ -0,0 +1,185 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntitySoftAdded + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + audit.Configuration.SoftAdded(x => true); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftAdded, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftDeleted/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftDeleted/Entity_Proxy.cs new file mode 100644 index 00000000..540f6678 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Audit/EntitySoftDeleted/Entity_Proxy.cs @@ -0,0 +1,186 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +#if EF5 || EF6 +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +#if EF5 +using System.Data.Entity; +using System.Data.Objects; + +#elif EF6 +using System.Data.Entity; +using System.Data.Entity.Core.Objects; + +#elif EFCORE +using Microsoft.Data.Entity; + +#endif + +namespace Z.Test.EntityFramework.Plus +{ + public partial class Audit_EntitySoftDeleted + { + [TestMethod] + public void Entity_Proxy() + { + var identitySeed = TestContext.GetIdentitySeed(x => x.Entity_Proxies); + + TestContext.DeleteAll(x => x.AuditEntryProperties); + TestContext.DeleteAll(x => x.AuditEntries); + TestContext.DeleteAll(x => x.Entity_Proxies); + + TestContext.Insert(x => x.Entity_Proxies, 3); + + var audit = AuditHelper.AutoSaveAudit(); + audit.Configuration.SoftDeleted(x => true); + + using (var ctx = new TestContext()) + { + var list = ctx.Entity_Proxies.ToList(); + + foreach (var item in list) + { + // ENSURE it's proxy + Assert.AreNotEqual(item.GetType(), ObjectContext.GetObjectType(item.GetType())); + } + + list.ForEach(x => x.ColumnInt++); + + ctx.SaveChanges(audit); + } + + // UnitTest - Audit + { + var entries = audit.Entries; + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(identitySeed + 1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(identitySeed + 3, entries[2].Properties[propertyIndex].NewValue); + + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual(0, entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual(2, entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual(1, entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual(2, entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual(3, entries[2].Properties[propertyIndex].NewValue); + } + } + + // UnitTest - Audit (Database) + { + using (var ctx = new TestContext()) + { + // ENSURE order + var entries = ctx.AuditEntries.OrderBy(x => x.AuditEntryID).Include(x => x.Properties).ToList(); + entries.ForEach(x => x.Properties = x.Properties.OrderBy(y => y.AuditEntryPropertyID).ToList()); + + // Entries + { + // Entries Count + Assert.AreEqual(3, entries.Count); + + // Entries State + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[0].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[1].State); + Assert.AreEqual(AuditEntryState.EntitySoftDeleted, entries[2].State); + + // Entries EntitySetName + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[0].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[1].EntitySetName); + Assert.AreEqual(TestContext.TypeName(x => x.Entity_Proxies), entries[2].EntitySetName); + + // Entries TypeName + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[0].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[1].EntityTypeName); + Assert.AreEqual(typeof (Entity_Proxy).Name, entries[2].EntityTypeName); + } + + // Properties + { + var propertyIndex = -1; + + // Properties Count + Assert.AreEqual(2, entries[0].Properties.Count); + Assert.AreEqual(2, entries[1].Properties.Count); + Assert.AreEqual(2, entries[2].Properties.Count); + + // ID + propertyIndex = 0; + Assert.AreEqual("ID", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ID", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual((identitySeed + 1).ToString(), entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 2).ToString(), entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual((identitySeed + 3).ToString(), entries[2].Properties[propertyIndex].NewValue); + + // ColumnInt + propertyIndex = 1; + Assert.AreEqual("ColumnInt", entries[0].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[1].Properties[propertyIndex].PropertyName); + Assert.AreEqual("ColumnInt", entries[2].Properties[propertyIndex].PropertyName); + Assert.AreEqual("0", entries[0].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[1].Properties[propertyIndex].OldValue); + Assert.AreEqual("2", entries[2].Properties[propertyIndex].OldValue); + Assert.AreEqual("1", entries[0].Properties[propertyIndex].NewValue); + Assert.AreEqual("2", entries[1].Properties[propertyIndex].NewValue); + Assert.AreEqual("3", entries[2].Properties[propertyIndex].NewValue); + } + } + } + } + } +} + +#endif \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Enable.cs index be04a20d..e1d9c1d3 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Enable.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_ManyFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) { Assert.AreEqual(35, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Exclude.cs index 779078e6..7df9f085 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Exclude.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_ManyFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true, excludeInterface: true, excludeBaseClass: true, excludeBaseInterface: true)) { Assert.AreEqual(44, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Include.cs index 01c57d42..f71b2ff1 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Include.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/ManyFilter_Include.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_ManyFilter_Include() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false, includeInterface: true, includeBaseClass: true, includeBaseInterface: true)) { Assert.AreEqual(36, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Enable.cs index e51683d5..3afeb3a5 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Enable.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_SingleFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { Assert.AreEqual(44, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Exclude.cs index 8e77fd6d..dce6d3b8 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Exclude.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_SingleFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, excludeClass: true)) { Assert.AreEqual(45, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Include.cs index 7ac3fda4..ee92f30c 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Include.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter/SingleFilter_Include.cs @@ -15,6 +15,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_SingleFilter_Include() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, includeClass: true)) { Assert.AreEqual(44, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_Enable.cs index a2d5c0a2..f16493c1 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_ManyFilte_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); @@ -27,7 +30,7 @@ public void WithGlobalFilter_WithInstanceFilter_ManyFilte_Enable() ctx.Filter(QueryFilterHelper.Filter.Filter6).Enable(); ctx.Filter(QueryFilterHelper.Filter.Filter7).Enable(); ctx.Filter(QueryFilterHelper.Filter.Filter8).Enable(); - + Assert.AreEqual(9, ctx.Inheritance_Interface_Entities.Sum(x => x.ColumnInt)); } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_Enable.cs index e5d1ad53..7d2a52ab 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_SingleFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Enable.cs index 23dd1e1d..1717142b 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_ManyFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Exclude.cs index 6875b05a..c567dc11 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Exclude.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_ManyFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true, excludeInterface: true, excludeBaseClass: true, excludeBaseInterface: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Include.cs index 0d5fade2..914f1d7b 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Include.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/ManyFilter_Include.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_ManyFilter_Include() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false, includeInterface: true, includeBaseClass: true, includeBaseInterface: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Enable.cs index c572e3a4..92a761a4 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_SingleFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Exclude.cs index 4488d89c..f62c6a1a 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Exclude.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_SingleFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true, excludeClass: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Include.cs index 68b71683..73777b53 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Include.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithGlobalManagerFilter/SingleFilter_Include.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithGlobalManagerFilter_SingleFilter_Include() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(true, enableFilter1: false, includeClass: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter.cs new file mode 100644 index 00000000..fab88559 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_ManyFilter() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Disable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Disable.cs new file mode 100644 index 00000000..8d12ca26 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Disable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Disable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Enable.cs new file mode 100644 index 00000000..a3c17a79 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Enable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Enable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Exclude.cs new file mode 100644 index 00000000..cceaeade --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Exclude.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Exclude() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true, excludeInterface: true, excludeBaseClass: true, excludeBaseInterface: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Include.cs new file mode 100644 index 00000000..6edb67cb --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/ManyFilter_Include.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Include() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false, includeInterface: true, includeBaseClass: true, includeBaseInterface: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(36, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter.cs new file mode 100644 index 00000000..29ad5b6b --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_SingleFilter() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Disable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Disable.cs new file mode 100644 index 00000000..a426e0c9 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Disable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Disable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Enable.cs new file mode 100644 index 00000000..e79058bb --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Enable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Enable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Exclude.cs new file mode 100644 index 00000000..7203f140 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Exclude.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Exclude() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, excludeClass: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Include.cs new file mode 100644 index 00000000..c6a0b4b6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInclude/SingleFilter_Include.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Include() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, includeClass: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Enable.cs index 5ab153e6..02208cd7 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithInstanceFilter_ManyFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Exclude.cs index 5d9aa8a6..8475c29b 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/ManyFilter_Exclude.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithInstanceFilter_ManyFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Enable.cs index 48f78dd9..b84c876a 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Enable.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Enable.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithInstanceFilter_SingleFilter_Enable() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Exclude.cs index e5df148d..e3982534 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Exclude.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithInstanceFilter/SingleFilter_Exclude.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbContext_Filter [TestMethod] public void WithInstanceFilter_SingleFilter_Exclude() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter.cs new file mode 100644 index 00000000..24fcaf18 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Disable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Disable.cs new file mode 100644 index 00000000..f707d247 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Disable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Disable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Enable.cs new file mode 100644 index 00000000..0c55f568 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Enable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Enable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Exclude.cs new file mode 100644 index 00000000..34abebc6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Exclude.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Exclude() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true, excludeInterface: true, excludeBaseClass: true, excludeBaseInterface: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Include.cs new file mode 100644 index 00000000..cd2def55 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/ManyFilter_Include.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Include() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false, includeInterface: true, includeBaseClass: true, includeBaseInterface: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(36, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter.cs new file mode 100644 index 00000000..263d1fbb --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Disable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Disable.cs new file mode 100644 index 00000000..089ef5ba --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Disable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Disable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Enable.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Enable.cs new file mode 100644 index 00000000..09cd901c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Enable.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Enable() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Exclude.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Exclude.cs new file mode 100644 index 00000000..4bd565e2 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Exclude.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Exclude() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, excludeClass: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Include.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Include.cs new file mode 100644 index 00000000..4163e240 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbContext_Filter/WithLazyLoading/SingleFilter_Include.cs @@ -0,0 +1,41 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbContext_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Include() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, includeClass: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.First().Rights; + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/ManyFilter_Enabled.cs index 1b817b7d..fc6e7109 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) { Assert.AreEqual(45, ctx.Inheritance_Interface_Entities.AsNoFilter().Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/SingleFilter_Enabled.cs index 2bf3351b..f96da89d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { Assert.AreEqual(45, ctx.Inheritance_Interface_Entities.AsNoFilter().Sum(x => x.ColumnInt)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs index a974f9e4..d1656f0d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs index ec3a3729..c7e15c96 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs index 33e496b3..6379480a 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs index 85f195dc..eb89fe2d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/ManyFilter_Enabled.cs index 3c84a6ec..b2568a91 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalManagerFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/SingleFilter_Enabled.cs index 54cbde20..72c20b2d 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithGlobalManagerFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithGlobalManagerFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Disabled.cs new file mode 100644 index 00000000..87cf6fa8 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Disabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithInclude_ManyFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Enabled.cs new file mode 100644 index 00000000..288b49ab --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/ManyFilter_Enabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithInclude_ManyFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Disabled.cs new file mode 100644 index 00000000..4614b032 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Disabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithInclude_SingleFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Enabled.cs new file mode 100644 index 00000000..60b91f52 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInclude/SingleFilter_Enabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithInclude_SingleFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/ManyFilter_Enabled.cs index fdba6536..619a3b28 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithInstanceFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/SingleFilter_Enabled.cs index 4250b403..cde2dc5b 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithInstanceFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_AsNoFilter [TestMethod] public void WithInstanceFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Disabled.cs new file mode 100644 index 00000000..29809e20 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Disabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().First().Rights; + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Enabled.cs new file mode 100644 index 00000000..8607065a --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/ManyFilter_Enabled.cs @@ -0,0 +1,44 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().First().Rights; + + // STILL filter LazyLoading + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Disabled.cs new file mode 100644 index 00000000..1b61edc5 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Disabled.cs @@ -0,0 +1,42 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().First().Rights; + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Enabled.cs new file mode 100644 index 00000000..a36eb01b --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_AsNoFilter/WithLazyLoading/SingleFilter_Enabled.cs @@ -0,0 +1,44 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_AsNoFilter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.AsNoFilter().First().Rights; + + // STILL filter LazyLoading + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/ManyFilter_Enabled.cs index b899c266..6605d0c4 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) { Assert.AreEqual(35, ctx.Inheritance_Interface_Entities.Filter( diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/SingleFilter_Enabled.cs index cc2a6c10..e81cd9f4 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { Assert.AreEqual(44, ctx.Inheritance_Interface_Entities.Filter( diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs index 3e494160..6715d671 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_ManyFilter_GlobalFilterDisabled_InstanceFilterDisabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs index 141f76df..dca0d47f 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_ManyFilter_GlobalFilterDisabled_InstanceFilterEnabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs index 0b5bbcc3..52043f97 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_SingleFilter_GlobalFilterEnabled_InstanceFilterDisabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5)); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs index a680abe1..4f3d05c5 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalFilter_WithInstanceFilter/SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalFilter_WithInstanceFilter_SingleFilter_GlobalFilterEnabled_InstanceFilterEnabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext(false, enableFilter1: true)) { ctx.Filter(QueryFilterHelper.Filter.Filter5, entities => entities.Where(x => x.ColumnInt != 5), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/ManyFilter_Enabled.cs index c0212183..5429a0be 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalManagerFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/SingleFilter_Enabled.cs index fef020b0..f2e88ffd 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithGlobalManagerFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithGlobalManagerFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { QueryFilterHelper.CreateGlobalManagerFilter(false, enableFilter1: true); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Disabled.cs new file mode 100644 index 00000000..de8f4d52 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Disabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Enabled.cs new file mode 100644 index 00000000..1389031e --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/ManyFilter_Enabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithInclude_ManyFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Disabled.cs new file mode 100644 index 00000000..14280cb0 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Disabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Enabled.cs new file mode 100644 index 00000000..b3b1f00c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInclude/SingleFilter_Enabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithInclude_SingleFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).Include(x => x.Rights).ToList().SelectMany(x => x.Rights); + + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/ManyFilter_Enabled.cs index 2a8c3950..f23f5c2c 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/ManyFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/ManyFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithInstanceFilter_ManyFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/SingleFilter_Enabled.cs index b67aaaae..8f39292b 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/SingleFilter_Enabled.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithInstanceFilter/SingleFilter_Enabled.cs @@ -16,6 +16,9 @@ public partial class QueryFilter_DbSet_Filter [TestMethod] public void WithInstanceFilter_SingleFilter_Enabled() { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.Insert(x => x.Inheritance_Interface_Entities, 10); + using (var ctx = new TestContext()) { ctx.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), false); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Disabled.cs new file mode 100644 index 00000000..5b06a2a4 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Disabled.cs @@ -0,0 +1,48 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false, enableFilter2: false, enableFilter3: false, enableFilter4: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).First().Rights; + + // STILL filter LazyLoading + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Enabled.cs new file mode 100644 index 00000000..ffe6dd36 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/ManyFilter_Enabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithLazyLoading_ManyFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true, enableFilter2: true, enableFilter3: true, enableFilter4: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).First().Rights; + + Assert.AreEqual(35, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Disabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Disabled.cs new file mode 100644 index 00000000..14892d2c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Disabled.cs @@ -0,0 +1,48 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Disabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(true, enableFilter1: false)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).First().Rights; + + // STILL filter LazyLoading + Assert.AreEqual(45, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Enabled.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Enabled.cs new file mode 100644 index 00000000..af69a590 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/QueryFilter/DbSet_Filter/WithLazyLoading/SingleFilter_Enabled.cs @@ -0,0 +1,47 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; +using System.Data.Entity; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public partial class QueryFilter_DbSet_Filter + { + [TestMethod] + public void WithLazyLoading_SingleFilter_Enabled() + { + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities); + TestContext.DeleteAll(x => x.Inheritance_Interface_Entities_LazyLoading); + + using (var ctx = new TestContext()) + { + var list = TestContext.Insert(ctx, x => x.Inheritance_Interface_Entities, 10); + + var left = ctx.Inheritance_Interface_Entities_LazyLoading.Add(new Inheritance_Interface_Entity_LazyLoading()); + left.Rights = new Collection(); + + list.ForEach(x => left.Rights.Add(x)); + ctx.SaveChanges(); + } + + using (var ctx = new TestContext(false, enableFilter1: true)) + { + var rights = ctx.Inheritance_Interface_Entities_LazyLoading.Filter( + QueryFilterHelper.Filter.Filter1, + QueryFilterHelper.Filter.Filter2, + QueryFilterHelper.Filter.Filter3, + QueryFilterHelper.Filter.Filter4).First().Rights; + + Assert.AreEqual(44, rights.Sum(x => x.ColumnInt)); + } + } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/Z.Test.EntityFramework.Plus.EF6.csproj b/src/test/Z.Test.EntityFramework.Plus.EF6/Z.Test.EntityFramework.Plus.EF6.csproj index 1aa55a7e..b95375c8 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/Z.Test.EntityFramework.Plus.EF6.csproj +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/Z.Test.EntityFramework.Plus.EF6.csproj @@ -72,6 +72,7 @@ + @@ -79,11 +80,27 @@ + + + + + + + + + + + + + + + + @@ -118,6 +135,12 @@ + + + + + + @@ -260,6 +283,9 @@ + + + @@ -303,6 +329,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -329,6 +375,14 @@ + + + + + + + + @@ -350,10 +404,18 @@ + + + + + + + + @@ -467,6 +529,8 @@ + + diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/AuditHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/AuditHelper.cs index a52d6818..46c2f36c 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/AuditHelper.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/AuditHelper.cs @@ -5,6 +5,10 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; using Z.EntityFramework.Plus; namespace Z.Test.EntityFramework.Plus @@ -12,10 +16,51 @@ namespace Z.Test.EntityFramework.Plus public static class AuditHelper { public static Audit AutoSaveAudit() + { + var audit = new Audit(); + audit.Configuration.AutoSavePreAction = (context, audit1) => (context as TestContext).AuditEntries.AddRange(audit1.Entries); + return audit; + } + + public static Audit AutoSaveWithAuditEntryFactory() + { + var audit = new Audit(); + audit.Configuration.AutoSavePreAction = (context, audit1) => + { + (context as TestContext).AuditEntry_Extendeds.AddRange(audit1.Entries.Cast()); + }; + + audit.Configuration.AuditEntryFactory = args => + { + return new AuditEntry_Extended + { + CreatedBy = "CustomCreatedBy", + CreatedDate = new DateTime(1981, 04, 13), + EntitySetName = "CustomEntitySetName", + EntityTypeName = "CustomEntityTypeName", + ExtendedValue = "CustomExtendedValue" + }; + }; + + return audit; + } + + public static Audit AutoSaveWithAuditEntryPropertyFactory() { var audit = new Audit(); audit.CreatedBy = "ZZZ Projects"; audit.Configuration.AutoSavePreAction = (context, audit1) => (context as TestContext).AuditEntries.AddRange(audit1.Entries); + + audit.Configuration.AuditEntryPropertyFactory = args => + { + return new AuditEntryProperty_Extended + { + RelationName = "CustomRelationName", + PropertyName = "CustomPropertyName", + ExtendedValue = "CustomExtendedValue" + }; + }; + return audit; } } diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/QueryFilterHelper.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/QueryFilterHelper.cs index 9b4f9b45..cf725c53 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/QueryFilterHelper.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Helper/QueryFilterHelper.cs @@ -5,6 +5,7 @@ // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. +using System; using System.Linq; using Z.EntityFramework.Plus; @@ -24,14 +25,23 @@ public enum Filter Filter8 } + public static object SingleThreadLock = new Object(); public static void ClearGlobalManagerFilter() { + +#if EF5 QueryFilterManager.GlobalFilters.Clear(); QueryFilterManager.GlobalInitializeFilterActions.Clear(); +#elif EF6 + QueryFilterManager.GlobalFiltersByKey.Clear(); + QueryFilterManager.GlobalFilterByType.Clear(); +#endif + } public static void CreateGlobalManagerFilter(bool isEnabled, string fixResharper = null, bool? enableFilter1 = null, bool? enableFilter2 = null, bool? enableFilter3 = null, bool? enableFilter4 = null, bool? excludeClass = null, bool? excludeInterface = null, bool? excludeBaseClass = null, bool? excludeBaseInterface = null, bool? includeClass = null, bool? includeInterface = null, bool? includeBaseClass = null, bool? includeBaseInterface = null) { + ClearGlobalManagerFilter(); #if EFCORE // TODO: Remove this when cast issue will be fixed QueryFilterManager.GlobalFilters.Clear(); diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntryProperty_Extended.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntryProperty_Extended.cs new file mode 100644 index 00000000..8c7f7fc0 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntryProperty_Extended.cs @@ -0,0 +1,9 @@ +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public class AuditEntryProperty_Extended : AuditEntryProperty + { + public string ExtendedValue { get; set; } + } +} diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntry_Extended.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntry_Extended.cs new file mode 100644 index 00000000..a5f84a8c --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/AuditEntry_Extended.cs @@ -0,0 +1,9 @@ +using Z.EntityFramework.Plus; + +namespace Z.Test.EntityFramework.Plus +{ + public class AuditEntry_Extended : AuditEntry + { + public string ExtendedValue { get; set; } + } +} diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy.cs new file mode 100644 index 00000000..7b4011a5 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; + +namespace Z.Test.EntityFramework.Plus +{ + public class Entity_Proxy + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public virtual ICollection Rights { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy_Right.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy_Right.cs new file mode 100644 index 00000000..4ff754e6 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Entity_Proxy_Right.cs @@ -0,0 +1,11 @@ +namespace Z.Test.EntityFramework.Plus +{ + public class Entity_Proxy_Right + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public Entity_Proxy Left { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Inheritance_Interface_Entity_LazyLoading.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Inheritance_Interface_Entity_LazyLoading.cs new file mode 100644 index 00000000..ea4b8437 --- /dev/null +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/Inheritance_Interface_Entity_LazyLoading.cs @@ -0,0 +1,20 @@ +// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit) +// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus +// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues +// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE +// More projects: http://www.zzzprojects.com/ +// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. + +using System.Collections.ObjectModel; + +namespace Z.Test.EntityFramework.Plus +{ + public class Inheritance_Interface_Entity_LazyLoading + { + public int ID { get; set; } + + public int ColumnInt { get; set; } + + public virtual Collection Rights { get; set; } + } +} \ No newline at end of file diff --git a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/_TestContext.cs b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/_TestContext.cs index 15074b10..a67834e2 100644 --- a/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/_TestContext.cs +++ b/src/test/Z.Test.EntityFramework.Plus.EF6/_Model/_TestContext.cs @@ -6,12 +6,15 @@ // Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved. using System.Data.Common; -using System.Data.Entity.Infrastructure.Interception; using System.Data.SqlClient; using System.Linq; using Z.EntityFramework.Plus; -#if EF5 || EF6 +#if EF5 +using System.Data.Entity; + +#elif EF6 using System.Data.Entity; +using System.Data.Entity.Infrastructure.Interception; #elif EFCORE using System.Configuration; @@ -61,6 +64,10 @@ public TestContext() Database.EnsureCreated(); #endif +#if EF6 + // BE careful, this one also need to clear filter! + QueryFilterManager.ClearQueryCache(this); +#endif } @@ -82,6 +89,12 @@ public TestContext(bool isEnabled, string fixResharper = null, bool? enableFilte QueryFilterManager.GlobalInitializeFilterActions.Clear(); #endif +#if EF6 + // Clear query cache + QueryFilterManager.ClearQueryCache(this); +#endif + + if (enableFilter1 != null) { this.Filter(QueryFilterHelper.Filter.Filter1, entities => entities.Where(x => x.ColumnInt != 1), isEnabled); @@ -317,6 +330,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet AuditEntryProperties { get; set; } + + public DbSet AuditEntry_Extendeds { get; set; } + + public DbSet AuditEntryProperty_Extendeds { get; set; } + #endregion #region Entity @@ -338,6 +356,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Entity_ManyGuids { get; set; } + public DbSet Entity_Proxies { get; set; } + + public DbSet Entity_Proxy_Rights { get; set; } + #if EF5 || EF6 public DbSet Entity_Complexes { get; set; } @@ -350,6 +372,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) public DbSet Inheritance_Interface_Entities { get; set; } + public DbSet Inheritance_Interface_Entities_LazyLoading { get; set; } + #if EF5 || EF6 public DbSet Inheritance_TPC_Animals { get; set; } diff --git a/version.txt b/version.txt index b1ad222d..ec7b9678 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.3.12 \ No newline at end of file +v1.4.0 \ No newline at end of file