Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soft deletes, navigation properties in EF4 CTP5 POCO

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?

like image 271
James Harris Avatar asked Jan 05 '11 09:01

James Harris


1 Answers

Maybe You should look at this from another perspective. Might help. Certainly won't hurt. :)

like image 75
Arnis Lapsa Avatar answered Oct 21 '22 18:10

Arnis Lapsa