EF4 CTP5 POCO 中的软删除、导航属性
基本上,我想使用软删除,但导航属性不显示软删除记录。有没有办法拦截实体框架中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许你应该从另一个角度来看这个。可能有帮助。当然不会受伤。 :)
Maybe You should look at this from another perspective. Might help. Certainly won't hurt. :)