Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF core将其他表项作为一个json列获取_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# EF core将其他表项作为一个json列获取

C# EF core将其他表项作为一个json列获取,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我有一个在SQL中定义的名为Blog的表: create table Blog ( SurrogateKey uniqueidentifier not null primary key, Text nvarchar(max) ) 和第二个名为Comments的表: create table Comments ( SurrogateKey uniqueidentifier not null primary key, Comment nvarchar(max) ) 在EF中,我有

我有一个在SQL中定义的名为Blog的表:

create table Blog
(
  SurrogateKey uniqueidentifier not null primary key,
  Text nvarchar(max)
)
和第二个名为Comments的表:

create table Comments
(
  SurrogateKey uniqueidentifier not null primary key,
  Comment nvarchar(max)
)
在EF中,我有实体博客和评论,如:

然后,我得到了与评论数量相同的所有结果。对于性能,我希望注释作为字符串。而且应该可以查询评论。这样我就得到了博客和所有评论的一个结果。我会用SQL写:

select SurrogateKey, Text, 
Comment = (select Comment 
           from comments where surrogateKey = blog.SurrogateKey 
           AND Comment like '%test%' FOR XML)
from Blog
Enity Framework(3)core是否也可以这样做?这样我就可以把博客的评论转换成字符串了?
转换应该在服务器端完成,而不是我稍后将comments对象转换为字符串的查询。

您可以使用在模型中注册SQL视图。

我不确定它是否解决了所有问题。有了你的回答,我明白了,我没有明确表示我也想过滤评论。我把这一点补充到问题中。适合这个无钥匙的enity?目前我还没有让它运行。我如何才能通过调查,获得适合博客的评论?
internal class BlogContext : DBContext
{
  ....

  protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
     modelBuilder.Entity<Blog>().HasKey(blog => blog.SurrogateKey);
     modelBuilder.Entity<Blog>().ToTable("Blog");
     modelBuilder.Entity<Comment>().HasKey(comment=> comment.SurrogateKey);
     modelBuilder.Entity<Comment>().ToTable("Comment");
     modelBuilder.Entity<Blog>().HasMany(item => item.Comments)
             .WithOne().HasForeignKey(comment => comment.SurrogateKey);
  }
}
select SurrogateKey, Text, Comment from Blog
join Comments on Blog.SurrogateKey = Comments.SurrogateKey 
select SurrogateKey, Text, 
Comment = (select Comment 
           from comments where surrogateKey = blog.SurrogateKey 
           AND Comment like '%test%' FOR XML)
from Blog