EF4 CTP5 POCO 中的软删除、导航属性

发布于 2024-10-10 06:55:39 字数 931 浏览 1 评论 0原文

基本上,我想使用软删除,但导航属性不显示软删除记录。有没有办法拦截实体框架中 POCO 对象的导航属性查询?

非常简单的示例:

 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;}
}

我可以轻松地将条件插入到我的存储库中,这样它就不会返回 IsDeleted==true 的任何产品。

但是,我不知道如何为导航属性中具有“软删除”实体的其他对象完成此操作。

IE 如果我访问 myCategory.Products (其中 myCategory 是一个类别),它不应该显示任何 IsDeleted==true 的产品

我可以使用类别的附加属性来解决此问题

public ICollection<Product> CurrentProducts
{
    get
    {
         return this.Products.Where(p=>!p.IsDeleted);
    }
}

但这不是我正在寻找的优雅解决方案。有没有办法将标准“附加”到导航属性或有更好的解决方案来处理这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染年凉城似染瑾 2024-10-17 06:55:39

也许你应该从另一个角度来看这个。可能有帮助。当然不会受伤。 :)

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

梅窗月明清似水 2024-10-17 06:55:39
public class CategoryWithNoDeletedItems : Category
{
    private ICollection<Product> _products;
    public override ICollection<Product> Products
    {
        get
        {
            return _products;
        }
        set
        {
            if (value.Any(x => x.IsDeleted))
            {
                _products = value.Where(x => !x.IsDeleted).ToArray();
            }
            else
            {
                _products = value;
            }
        }
    }
}
public class CategoryWithNoDeletedItems : Category
{
    private ICollection<Product> _products;
    public override ICollection<Product> Products
    {
        get
        {
            return _products;
        }
        set
        {
            if (value.Any(x => x.IsDeleted))
            {
                _products = value.Where(x => !x.IsDeleted).ToArray();
            }
            else
            {
                _products = value;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文