Basically, I want to use soft deletes, but have the navgiation properties not show the soft deleted records. Are there any ways to intercept the navigation property queries on POCO objects in entity framework?
Very simple example:
public class Product
{
public int Id { get; set;}
public string Name { get; set;}
public int? CategoryId { get; set;}
public virtual Category Category { get; set;}
public bool IsDeleted { get; set;}
}
public class Category
{
public int Id{ get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set;}
}
I can easily insert the criteria into my repository so that it doesn't return any products where IsDeleted==true.
However, I can't see how to accomplish this for other objects that have 'soft deleted' entites in their navigation properties.
IE If I access myCategory.Products (where myCategory is a Category) it should not show any products where IsDeleted==true
I could potentially workaround this using an additional property of Category
public ICollection<Product> CurrentProducts
{
get
{
return this.Products.Where(p=>!p.IsDeleted);
}
}
But that isn't the elegant solution I'm looking for. Is there a way to 'attach' criteria to the navigation property or any better solutions for how to handle this?
Maybe You should look at this from another perspective. Might help. Certainly won't hurt. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With