C# .NET core 3.1 API中表之间的一对多关系返回空列表对象
我正在制作dotnetapi。 我有类主题和类标签。 一个主题将有多个标记,而一个标记只包含一个主题。 当我使用postman:运行时,对于getAllTopic,它返回空的标记列表,并且 运行时:,对于getAllTag,未返回主题的null对象 这是我的密码 类别:标签C# .NET core 3.1 API中表之间的一对多关系返回空列表对象,c#,.net,api,C#,.net,Api,我正在制作dotnetapi。 我有类主题和类标签。 一个主题将有多个标记,而一个标记只包含一个主题。 当我使用postman:运行时,对于getAllTopic,它返回空的标记列表,并且 运行时:,对于getAllTag,未返回主题的null对象 这是我的密码 类别:标签 using System.ComponentModel.DataAnnotations; namespace NewAndNotificationAPI.Models{ public class Tag{
using System.ComponentModel.DataAnnotations;
namespace NewAndNotificationAPI.Models{
public class Tag{
[Key]
public int tagId { get; set; }
[Required]
public string name { get; set; }
[Required]
public string status { get; set; }
[Required]
public string description { get; set; }
[Required]
public int topicId { get; set; }
[Required]
public virtual Topic topic {get;set;}
}
}
类别:主题
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace NewAndNotificationAPI.Models{
public class Topic{
[Key]
public int topicId { get; set; }
[Required]
public string name { get; set; }
[Required]
public string status { get; set; }
[Required]
public string description { get; set; }
public virtual List<Tag> tags{get;set;} =new List<Tag>();
}
}
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
名称空间NewAndNotificationAPI.Models{
公开课主题{
[关键]
公共int-topicId{get;set;}
[必需]
公共字符串名称{get;set;}
[必需]
公共字符串状态{get;set;}
[必需]
公共字符串说明{get;set;}
公共虚拟列表标记{get;set;}=new List();
}
}
类别:数据库上下文:
using Microsoft.EntityFrameworkCore;
using NewAndNotificationAPI.Models;
namespace NewAndNotificationAPI.Data{
public class NewAndNotificationContext:DbContext{
public NewAndNotificationContext(DbContextOptions<NewAndNotificationContext> opt):base(opt){
}
public DbSet<Topic> Topics{get;set;}
public DbSet<Tag> Tags{get;set;}
protected override void OnModelCreating(ModelBuilder modelBuilder){
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Tag>()
.HasOne<Topic>(tg => tg.topic)
.WithMany(t => t.tags )
.HasForeignKey(tg => tg.topicId);
}
}
}
使用Microsoft.EntityFrameworkCore;
使用NewAndNotificationAPI.Models;
命名空间NewAndNotificationAPI.Data{
公共类NewAndNotificationContext:DbContext{
public NewAndNotificationContext(DbContextOptions opt):基本(opt){
}
公共数据库集主题{get;set;}
公共DbSet标记{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder){
基于模型创建(modelBuilder);
modelBuilder.Entity()
.HasOne(tg=>tg.topic)
.WithMany(t=>t.tags)
.HasForeignKey(tg=>tg.topicId);
}
}
}
请帮帮我