2

(对 SO 并不陌生,但对提问很陌生,所以请原谅错误。谢谢。)

我使用 EF Power Tools 预生成视图,但后来我return null;在生成的类的以下方法的末尾看到了“”,所以我想知道何时(或是否)GetView实际返回 null。

这是方法:

    /// <summary>
    /// Gets a view corresponding to the specified extent.
    /// </summary>
    /// <param name="extent">The extent.</param>
    /// <returns>The mapping view, or null if the extent is not associated with a mapping view.</returns>
    public override DbMappingView GetView(EntitySetBase extent)
    {
        if (extent == null)
        {
            throw new ArgumentNullException("extent");
        }

        var extentName = extent.EntityContainer.Name + "." + extent.Name;

       //...

       if (extentName == "CodeFirstDatabase.Post")
        {
            return GetView22();
        }

        if (extentName == "CodeFirstDatabase.PostComment")
        {
            return GetView23();
        }

        //...

        return null;
    }

这是我的模型:

public class Post
{
    public Post()
    {
        comments = new HashSet<PostComment>();
    }

    //...

    [InverseProperty("post")]
    public virtual ICollection<PostComment> comments { get; set; }

    //...
}

public class PostComment
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    [ForeignKey("comment")]
    public long comment_id { get; set; }
    public Post comment { get; set; }

    [ForeignKey("post")]
    public long post_id { get; set; }
    [InverseProperty("comments")]
    public Post post { get; set; }
}

我的帖子和评论具有相同的模型(毕竟它们只是存储文本)。我使用该PostComment模型将评论分组在一个单独的表中,以便对评论的查询可以更快,因为它们不包括实际帖子。

return null;在方法的“ ”行设置断点GetView让我“ CodeFirstDatabase.PostComment_comment”为extentName,我认为这是指PostComment模型上的注释外键。当然,它返回 null 因为 EF Power Tool 没有为它生成视图。

我的问题是:

  1. 为什么 EF Power Tools 没有为该关系(或任何其他关系)生成视图?
  2. 返回 null 会使 EF 在运行时生成视图本身吗?
  3. 如果是 2,这会影响第一次查询的启动时间吗?

作为相关的第四个问题,为什么CodeFirstDatabase在视图生成中名称“”与实际上下文名称一起使用?

我正在使用 EntityFramework 6.1.3 顺便说一句。

4

0 回答 0