1

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?

2
  • Are you using EF? Commented Nov 25, 2017 at 8:00
  • Yes I am using EF. Commented Nov 26, 2017 at 6:29

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I am using EF, will try that later.
Tried your answer. Do I need to update migrations or anything? Cause I get the following error: Schema specified is not valid. Errors: error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=True,DefaultValue=,Precision=]' of member 'DeletedAt' in type 'MyModel' is not compatible with 'SqlServer.bit[Nullable=True,DefaultValue=]' of member 'DeletedAt' in type 'CodeFirstDatabaseSchema.MyModel'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.