我在我的Sqlite数据库中有四条博客记录和两条作者记录。当我尝试获取博客时,我让EF核心获取相关数据(在本例中是Author)并在我的Blog对象中设置Author对象。
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;}
}
}
我试过用延迟加载和渴望加载来实现这个功能,结果都是一样的,很有趣,我看了一些documentation,没有看到任何东西让我认为这是一个功能,但我很高兴被证明是错误的。
另外,不知道为什么我',得到$id:我的json中的x字段。也许这是使用Sqlite EF核心包的产物。如果你知道,我很想知道我如何解决这个问题!
1条答案
按热度按时间q43xntqr1#
我觉得很傻。我的两个问题似乎都是由我的Program.cs文件中的这个配置引起的:
在看了文档之后,Jeremy Lakeman评论说这一切都是有意义的。我已经删除了该配置并将其替换为:
谢谢你的帮助!