Reputation: 371
I am using code-first Entity Framework 6.0 and have implemented a soft delete solution based on https://putshello.wordpress.com/2014/08/20/entity-framework-soft-deletes-are-easy/. This works well and automatically ignores the records where IsDeleted
is true
. So my model builder has an entry similar to the following:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>()
.Map(m => m.Requires("IsDeleted").HasValue(false))
.Ignore(m => m.IsDeleted);
}
I now have a requirement to access the deleted records, but only in one section of the application. So I need to select the records where IsDeleted
is true.
If I attempt to simply use:
where company.IsDeleted = true
It will fail with the following message:
The specified type member 'IsDeleted' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
I have looked everywhere and there is plenty of information about Entity Framework Core 2.0, but nothing about EF6.1. Short of using SQL scripts with the old SqlClient, does anyone have any hints of how to access these records using linq to entities?
Upvotes: 2
Views: 2747
Reputation: 371
In the end, the only way to easily resolve this was to implement EntityFramework.DynamicFilters from https://github.com/zzzprojects/EntityFramework.DynamicFilters. This is a great solution and provides the flexibility to switch off a filter dynamically.
protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
modelBuilder.Filter( "IsDeleted", ( ISoftDelete d ) => d.IsDeleted, false ));
}
You then add an interface:
internal interface ISoftDelete
{
bool IsDeleted { get; set; }
}
Then, switch off (as required) the IsDeleted filter:
ctx.DisableFilter( "IsDeleted" );
Much easier!
Upvotes: 4