EF Core 不会多次加载相关数据

问题描述 投票:0回答:0

我的 Sqlite 数据库中有四个博客记录和两个作者记录。当我尝试获取博客时,我让 EF 核心获取相关数据(在本例中为作者)并在我的博客对象中设置作者对象。

public async Task<List<Blog>> GetAllBlogs(BlogContext context) 
{
    return context.Blogs.Include(a => a.Author).ToList();
}

这很好用,但似乎我只在整个数据集中返回作者的唯一实例。这是 GetAllBlogs 的 json 结果:

    {
  "$id": "1",
  "result": {
    "$id": "2",
    "$values": [
      {
        "$id": "3",
        "blogId": "77da9a11-f752-4260-bebd-c1dbf7031d13",
        "author": {
          "$id": "4",
          "authorId": "ced0627d-bb4f-4941-a718-dd2438f0bf51",
          "authorFirstName": "John",
          "authorMiddleName": "R",
          "authorLastName": "Smith",
          "briefDescription": "Cool guy",
          "createdDate": "2023-01-10T22:22:51.7759933",
          "modifiedDate": null,
          "archivedDate": null
        },
        "blogTitle": "string",
        "blogContent": "string",
        "blogTags": "string",
        "createdDate": "2023-04-05T20:27:50.4621096",
        "modifiedDate": null,
        "archivedDate": null
      },
      {
        "$id": "5",
        "blogId": "96b0ae6d-d924-4099-87dd-5a8c7be50eed",
        "author": {
          "$id": "6",
          "authorId": "1d9ee13a-2581-44a4-94d6-b201506c4d29",
          "authorFirstName": "T",
          "authorMiddleName": "R",
          "authorLastName": "B",
          "briefDescription": "T B is a fullstack software developer with an emphesis on .NET and JavaScript technologies.",
          "createdDate": "2023-01-10T22:22:51.7759933",
          "modifiedDate": null,
          "archivedDate": null
        },
        "blogTitle": "asdfasdf",
        "blogContent": "asdfasfasfasdf",
        "blogTags": "",
        "createdDate": "2023-04-05T20:30:20.1779955",
        "modifiedDate": null,
        "archivedDate": null
      },
      {
        "$id": "7",
        "blogId": "494ea31d-40ea-4722-af56-3c2fb7aa260f",
        "author": {
          "$ref": "6"
        },
        "blogTitle": "asdf",
        "blogContent": "asf",
        "blogTags": "",
        "createdDate": "2023-04-05T20:38:14.3230626",
        "modifiedDate": null,
        "archivedDate": null
      },
      {
        "$id": "8",
        "blogId": "005ab54d-3bca-4d9d-8532-ca5613e8b5b7",
        "author": {
          "$ref": "6"
        },
        "blogTitle": "Title",
        "blogContent": "*content!",
        "blogTags": "",
        "createdDate": "2023-04-05T20:38:47.3545631",
        "modifiedDate": null,
        "archivedDate": null
      }
    ]
  }
}

我的两位作者确实出现在适当的博客记录下,但即使他们出现在其他记录下,他们也只出现一次。

下面是我的数据库上下文类:

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using backend.Models;

public class BlogContext : DbContext 
{

//protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
//=> optionsBuilder
//  .UseLazyLoadingProxies()
//  .UseSqlite("Data source=web.db");
    

public BlogContext(DbContextOptions<BlogContext> options) : base(options)
{
    
}
public DbSet<Blog> Blogs {get; set;}
public DbSet<Author> Authors {get; set;}

}

我的博客类:

using System.ComponentModel.DataAnnotations;

namespace backend.Models
{
    public class Blog : Times
    {
    [Key]
    public String BlogId {get; set;}
    public virtual Author Author { get; set; }
    public String BlogTitle {get; set;}
    public String BlogContent {get; set;}
    public String? BlogTags {get; set;}
    
    
    }

}

我的作者类:

using System.ComponentModel.DataAnnotations;

namespace backend.Models
{
    public class Author : Times
    {
    [Key]
    public String AuthorId {get; set;}
    public String AuthorFirstName {get; set;}
    public String? AuthorMiddleName {get; set;}
    public String AuthorLastName {get;set;}
    public String? BriefDescription {get; set;}
    }
}

我试过让它与延迟加载和急切加载一起工作,结果非常有趣。我查看了一些文档,没有看到任何让我认为这是一个功能的东西,但我很高兴被证明是错误的。

另外,不知道为什么我在我的 json 中获取 $id: x 字段。也许这是使用 Sqlite EF 核心包的神器。如果你知道我很想知道如何解决这个问题!

c# .net sqlite entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.