存储过程中未返回被忽略的属性

本文关键字:属性 返回 存储过程 过程中 存储 | 更新日期: 2023-09-27 18:15:35

我有一个博客模型和一个博客评论模型。这个博客有一个与它相关的博客评论的集合,但是当我得到我所有的博客,所以我可以列出他们在一些总结页面,我不想拉所有的评论,以及,我只需要评论当我选择一个特定的博客,去它的页面。但是,我确实希望注释的计数显示在摘要部分。

我创建了一个用于检索博客的存储过程,该过程不返回任何评论,但返回与博客相关的评论计数的值。

当选择一个特定的博客时,我将简单地让EF为我抓取它,而不是使用存储过程。

我遇到的问题是,我不希望评论计数是博客表中的一列,所以最初我认为我应该在使用流畅api的OnModelCreating方法中忽略它。然而,这意味着当我运行我的存储过程时,它没有返回我的存储过程应该给我的值。

博客类:

public class Blog
{
    public Guid BlogGuid { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }
    public string Author { get; set; }
    public DateTime DatePosted { get; set; }
    public IList<BlogComment> Comments { get; set; }
    public int CommentCount { get; set; }
    public Blog()
    {
        Comments = new List<BlogComment>();
    }
}

Blog Comment Class:

public class BlogComment
{
    public Guid BlogCommentGuid { get; set; }
    public Guid BlogGuid { get; set; }
    public int ContactRef { get; set; }
    public DateTime DatePosted { get; set; }
    public string Text { get; set; }
}

OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    var blog = modelBuilder.Entity<Blog>();
    blog.ToTable("Blogs");
    blog.HasKey(b => b.BlogGuid);
    blog.HasMany(b => b.Comments).WithRequired().HasForeignKey(c => c.BlogGuid);
    blog.Ignore(b => b.CommentCount);
    var blogComment = modelBuilder.Entity<BlogComment>();
    blogComment.ToTable("BlogComments");
    blogComment.HasKey(c => c.BlogCommentGuid);
}
获取博客方法:
public IList<Blog> GetBlogs()
{
    return context.Database.SqlQuery<Blog>("Get_Blogs").ToList();
}

存储过程代码:

CREATE TABLE #Blogs 
(
    BlogGuid uniqueidentifier, 
    Title nvarchar(50), 
    Text nvarchar(MAX), 
    Author nvarchar(50), 
    DatePosted datetime
)
IF (@BlogCount IS NOT NULL AND @BlogCount > 0)
BEGIN
    INSERT INTO #Blogs
        SELECT TOP (@BlogCount) *
            FROM Blogs
            ORDER BY DatePosted DESC
END ELSE BEGIN
    INSERT INTO #Blogs
        SELECT *
            FROM Blogs
            ORDER BY DatePosted DESC
END
SELECT *,
    (SELECT COUNT(*)
        FROM BlogComments
        WHERE BlogComments.BlogGuid = #Blogs.BlogGuid) CommentCount
FROM #Blogs
DROP TABLE #Blogs

调用GetBlogs()时,即使博客评论表中有评论,CommentCount也始终为零。

我想我明白为什么会这样,我只是想不出解决办法。

存储过程中未返回被忽略的属性

TBH,我只是在' CommentCount '的get中放置了一个Count调用。

public int CommentCount { get { return this.Comments.Count; } set; }

我相信我有一个解决方案,这是创建一个超类的博客称为BlogSummaryCommentCount属性在它。然后我在OnModelCreating方法中将其设置为忽略。

BlogSummary:

public class BlogSummary: Blog
{
    public int CommentCount { get; set; }
}

OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Blog
    var blog = modelBuilder.Entity<Blog>();
    blog.ToTable("Blogs");
    blog.HasKey(b => b.BlogGuid);
    blog.HasMany(b => b.Comments).WithRequired().HasForeignKey(c => c.BlogGuid);
    // Blog Summary
    modelBuilder.Ignore<BlogSummary>();
    // Blog Comment
    var blogComment = modelBuilder.Entity<BlogComment>();
    blogComment.ToTable("BlogComments");
    blogComment.HasKey(c => c.BlogCommentGuid);
}