簡體   English   中英

存儲過程中未返回被忽略的屬性

[英]Ignored property not returned in stored procedure

我有一個博客模型和一個博客評論模型。 該博客具有與之相關的博客評論集合,但是當我獲取所有博客以便可以在某種摘要頁面上列出它們時,我也不想提取所有評論,我只是選擇特定博客並轉到其頁面時,需要評論。 但是,我確實希望在摘要部分顯示評論的數量。

我創建了一個用於檢索博客的存儲過程,該存儲過程不返回任何評論,但確實返回與博客關聯的評論計數的值。

選擇一個特定的博客時,我只會讓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>();
    }
}

博客評論類別:

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,我只是把一個Count調用的get CommentCount的`。

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

我相信我有一個解決方案是創建名為博客的超BlogSummary它具有CommentCount它財產。 然后,我將其設置為在OnModelCreating方法中忽略。

博客摘要:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM