{"id":19214668,"url":"https://github.com/birajmainali/global-query-filter-for-softdelete","last_synced_at":"2025-07-04T23:32:27.775Z","repository":{"id":112771992,"uuid":"378197142","full_name":"BirajMainali/global-query-filter-for-softDelete","owner":"BirajMainali","description":"This is a demonstration of soft delete as well as a global query filter for soft-deleted items","archived":false,"fork":false,"pushed_at":"2021-09-19T13:37:59.000Z","size":675,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-04T18:13:07.781Z","etag":null,"topics":["ef-core","softdelete"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BirajMainali.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-18T15:44:39.000Z","updated_at":"2022-09-27T10:08:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"d587c4f0-8629-4975-bea6-5d3f668700bd","html_url":"https://github.com/BirajMainali/global-query-filter-for-softDelete","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BirajMainali%2Fglobal-query-filter-for-softDelete","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BirajMainali%2Fglobal-query-filter-for-softDelete/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BirajMainali%2Fglobal-query-filter-for-softDelete/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BirajMainali%2Fglobal-query-filter-for-softDelete/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BirajMainali","download_url":"https://codeload.github.com/BirajMainali/global-query-filter-for-softDelete/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240288187,"owners_count":19777626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ef-core","softdelete"],"created_at":"2024-11-09T14:10:53.775Z","updated_at":"2025-02-23T08:22:32.035Z","avatar_url":"https://github.com/BirajMainali.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SoftDelete\nThis is a demonstration of soft delete as well as a global query filter for soft deleted items\n\n``ApplicatonDbContext.cs``\n```c#\npublic override Task\u003cint\u003e SaveChangesAsync(bool acceptAllChangesOnSuccess,\n            CancellationToken cancellationToken = new CancellationToken())\n        {\n            foreach (var entry in ChangeTracker.Entries())\n            {\n                var entity = entry.Entity;\n                if (entry.State == EntityState.Deleted \u0026\u0026 entity is ISoftDelete)\n                {\n                    entry.State = EntityState.Modified;\n                    entity.GetType().GetProperty(\"RecStatus\")?.SetValue(entity, 'D');\n                }\n            }\n\n            return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);\n        }\n```\nNote : If we Only Check the  `EntityState.Deleted` So we need to add a filter every time to get Actual data like\n```c#\n_context.entity.where(x=\u003ex.RecStatus == 'A')\n```\nBut If we add the Global Filter like \n\n``ApplicationDbContext.cs``\n\n```c#\npublic void SetGlobalQuery\u003cT\u003e(ModelBuilder builder) where T : GenericModel\n        {\n            builder.Entity\u003cT\u003e().HasQueryFilter(e =\u003e e.RecStatus.Equals('A'));\n        }\n\n        static readonly MethodInfo SetGlobalQueryMethod = typeof(ApplicationDbContext)\n            .GetMethods(BindingFlags.Public | BindingFlags.Instance)\n            .Single(t =\u003e t.IsGenericMethod \u0026\u0026 t.Name == \"SetGlobalQuery\");\n\n        private void AddSafeDeleteGlobalQuery(ModelBuilder builder)\n        {\n            foreach (var type in builder.Model.GetEntityTypes())\n            {\n                if (type.BaseType == null \u0026\u0026 typeof(ISoftDelete).IsAssignableFrom(type.ClrType))\n                {\n                    var method = SetGlobalQueryMethod.MakeGenericMethod(type.ClrType);\n                    method.Invoke(this, new object[] {builder});\n                }\n            }\n        }\n```\nIt Execute every time `SetGlobalQuery`, so no need to add filter to get Actual Data\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbirajmainali%2Fglobal-query-filter-for-softdelete","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbirajmainali%2Fglobal-query-filter-for-softdelete","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbirajmainali%2Fglobal-query-filter-for-softdelete/lists"}