实体框架约束的导航属性 [英] Entity Framework constrains on navigation properties

查看:126
本文介绍了实体框架约束的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要限制由导航属性返回模式。例如,我使用的是 AuditInfo 模式登录模型的活动。一旦模型被删除 DeletedBy 删除属性设置。然而,由于没有什么人真正从数据库中删除,这些车型将仍然居住在其他车型中引用的导航属性。

I want to limit model that are returned by a navigational property. For example, I am using an AuditInfo model to log the activity of a model. Once a model is deleted the DeletedBy and Deleted attributes are set. However since nothing is ever really "deleted" from the database, these models will still be populated in navigational properties referenced by other models.

AuditInfo类

public class AuditInfo
{
    [Key]
    public int AuditInfoID { get; set; }

    //Other attributes

    public string DeletedBy { get; set; }

    public DateTime? Deleted { get; set; }
}

类具有导航性能

public class BlogPost
{
    //Other attributes

    //Only return Comment where Comment.AuditInfo.Deleted is NULL
    public virtual IList<Comment> Comments { get; set; }
}

类被审计

public class Comment
{
    //Other attributes

    public int AuditInfoID { get; set; }
}

我怎么会成立一个约束,以便只有非删除评论(Comment.AuditInfo.Deleted为NULL)从BlogPost.Comments?

How would I set up a constrain so that only the non-deleted comments (Comment.AuditInfo.Deleted is NULL) from a BlogPost.Comments?

推荐答案

(我假设,因为使用的是EF code-首先, [关键] 属性。)

(I assume you are using EF Code-First, because of the [Key] attribute.)

有不同的方式来加载导航属性和相关的实体,你可以申请一个过滤器,其中的一些方式,但不是所有的:

There are different ways to load navigation properties and related entities and you can apply a filter for some of these ways but not for all:

  • 延迟加载:

  • Lazy loading:

您导航酒店为虚拟使延迟加载在所有工作:

Your navigation property has to be virtual so that lazy loading works at all:

public virtual IList<Comment> Comments { get; set; }

加载父:

var blogPost = context.BlogPosts.Find(1);
foreach (var comment in blogPost.Comments) // lazy loading triggered here
{
}

您不能在此应用过滤器。延迟加载将永远载入的所有的给定的博客发表评论。

You can't apply a filter here. Lazy loading will always load all comments of the given blog post.

预先加载:

var blogPost = context.BlogPosts.Include(b => b.Comments)
    .SingleOrDefault(b => b.Id == 1);

您可以不适用过滤器包括。预先加载将永远载入的所有的给定的博客发表评论。

You can't apply a filter in Include. Eager loading will always load all comments of the given blog post.

显式加载:

加载父:

var blogPost = context.BlogPosts.Find(1);

当你现在加载注释可以应用过滤器:

You can apply a filter when you load the comments now:

context.Entry(blogPost).Collection(b => b.Comments).Query()
    .Where(c => !c.AuditInfo.Deleted.HasValue)
    .Load();

  • 投影:

  • Projection:

    您可以在投影性能应用过滤器:

    You can apply a filter in the projected properties:

    var blogPost = context.BlogPosts
        .Where(b => b.Id == 1)
        .Select(b => new
        {
            BlogPost = b,
            Comments = b.Comments.Where(c => !c.AuditInfo.Deleted.HasValue)
        })
        .SingleOrDefault();
    

  • 这是不可能使用某种形式的全球过滤策略的模型定义,因此,这个过滤器被申请的所有的上述自动和无明确装载明确规定,并在投影的方法例。 (我想你心里有这样一个全球模型定义,但是这是不可能的。)

    It is not possible to apply some kind of global filter policy in the model definition so that this filter gets applied for all methods above automatically and without specifying it explicitly in the explicit loading and in the projection example. (I think you have such a global model definition in mind, but that's not possible.)

    这篇关于实体框架约束的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆