简体   繁体   中英

C# Web API and SQL server global where clause

So i'm trying to implement a soft delete feature, and I have a DeletedAt field in almost all of my tables. My problem now is since this feature is not native to Web API , I would have to put a where clause of DeletedAt == null in all of my queries. My question would be, is there a way to put this where clause globally either in Web API or maybe if it's also possible from SQL Server?

I hope you are using Entity Framework. You can add a global filter at OnModelCreating event by adding a discriminator like:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<Foo>().Map(m => m.Requires("DeletedAt").HasValue(false));
   modelBuilder.Entity<Bar>().Map(m => m.Requires("DeletedAt").HasValue(false)); 
}

OR

You can use this package: https://github.com/zzzprojects/EntityFramework.DynamicFilters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM