已忽略 属性 未在存储过程中返回
Ignored property not returned in stored procedure
我有一个博客模型和一个博客评论模型。该博客有一组与之相关的博客评论,但是当我获取我所有的博客以便我可以在某种摘要页面上列出它们时,我也不想提取所有评论,我只当我 select 一个特定的博客并转到它的页面时需要评论。但是,我确实希望在摘要部分显示评论数。
我创建了一个用于检索博客的存储过程,它没有 return 任何评论,但 return 为与博客相关的评论计数提供了一个值。
当 select 浏览特定博客时,我只会让 EF 为我抓取它,而不是使用存储过程。
我遇到的问题是我不希望评论数成为博客中的一个专栏table所以最初我认为我应该在使用流利的 OnModelCreating 方法中忽略它api.但是,这意味着当我 运行 我的存储过程时,它不会 return 我的存储过程应该给我的值。
博客Class:
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>();
}
}
博客评论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; }
}
创建模型时:
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
始终为零,即使博客在博客评论 table 中有评论也是如此。
我想我明白为什么会这样,只是想不出解决办法。
TBH,我只是在 `CommentCount.
的 get
中调用 Count
public int CommentCount { get { return this.Comments.Count; } set; }
我相信我有一个解决方案,即为名为 BlogSummary
的博客创建一个超类,其中包含 CommentCount
属性。然后我在 OnModelCreating
方法中将其设置为忽略。
博客摘要:
public class BlogSummary: Blog
{
public int CommentCount { get; set; }
}
创建模型时:
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);
}
我有一个博客模型和一个博客评论模型。该博客有一组与之相关的博客评论,但是当我获取我所有的博客以便我可以在某种摘要页面上列出它们时,我也不想提取所有评论,我只当我 select 一个特定的博客并转到它的页面时需要评论。但是,我确实希望在摘要部分显示评论数。
我创建了一个用于检索博客的存储过程,它没有 return 任何评论,但 return 为与博客相关的评论计数提供了一个值。
当 select 浏览特定博客时,我只会让 EF 为我抓取它,而不是使用存储过程。
我遇到的问题是我不希望评论数成为博客中的一个专栏table所以最初我认为我应该在使用流利的 OnModelCreating 方法中忽略它api.但是,这意味着当我 运行 我的存储过程时,它不会 return 我的存储过程应该给我的值。
博客Class:
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>();
}
}
博客评论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; }
}
创建模型时:
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
始终为零,即使博客在博客评论 table 中有评论也是如此。
我想我明白为什么会这样,只是想不出解决办法。
TBH,我只是在 `CommentCount.
的get
中调用 Count
public int CommentCount { get { return this.Comments.Count; } set; }
我相信我有一个解决方案,即为名为 BlogSummary
的博客创建一个超类,其中包含 CommentCount
属性。然后我在 OnModelCreating
方法中将其设置为忽略。
博客摘要:
public class BlogSummary: Blog
{
public int CommentCount { get; set; }
}
创建模型时:
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);
}