繁体   English   中英

带有列表的 POST 方法<t> object 与 EF Core 5.0 API</t>

[英]POST method with List<T> object with EF Core 5.0 API

所以我一般是编码新手,开始使用 .net 核心 5.0。 我正在尝试使用 Entity Framework Core 开发一个简单的 API,但在尝试使用 POST 方法时遇到问题。 我有一个名为 Question 的 class 和另一个名为 Template 的 class,如下所示

public class Question
{
    public int Id { get; set; }
    public string Answer { get; set; }
    public int Weight { get; set; }
}

public class Template
{
    public int Id { get; set; }
    public List<Question> Questions { get; set; }
}

当我在 API 运行的情况下运行 go 到 Postman 时,我在 POST 方法中输入以下内容:

{
    "questions" : [
    {   "answer" : "a", "weight" : 2  },
    {   "answer" : "b", "weight" : 2  },
    {   "answer" : "c", "weight" : 2  },
    {   "answer" : "d", "weight" : 2  },
    {   "answer" : "a", "weight" : 1  },
    {   "answer" : "b", "weight" : 1  }
    ]
}

但是当我调用 GET 方法时,我得到的响应是这样的

[
   {
      "id": 1,
      "questions": null
   },
   {
      "id": 2,
      "questions": null
   }
]

这是我的 TemplatesController class 中带有 POST 和 GET 的代码部分

[Route("api/[controller]")]
[ApiController]
public class TemplatesController : ControllerBase
{
    private readonly AlfContext _context;

    public TemplatesController(AlfContext context)
    {
        _context = context;
    }

    // GET: api/Templates
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Template>>> GetTemplates()
    {
        return await _context.Templates.ToListAsync();
    }

    // GET: api/Templates/5
    [HttpGet("{id}")]
    public async Task<ActionResult<Template>> GetTemplate(int id)
    {
        var template = await _context.Templates.FindAsync(id);

        if (template == null)
        {
            return NotFound();
        }

        return template;
    }

    // POST: api/Templates
    [HttpPost]
    public async Task<ActionResult<Template>> PostTemplate(Template template)
    {
        _context.Templates.Add(template);
        await _context.SaveChangesAsync();

        return CreatedAtAction(nameof(GetTemplate), new { id = template.Id }, template);
    }

    private bool TemplateExists(int id)
    {
        return _context.Templates.Any(e => e.Id == id);
    }
}

这是我的 DbContext class

public class AlfContext : DbContext
{
    public DbSet<Template> Templates { get; set; }

    public AlfContext(DbContextOptions<AlfContext> options)
        : base(options)
    {
    }
} 

为什么我在“问题”字段中不断收到“null”作为响应? 我错过了什么,或者可能是一个概念?

根据您的问题,以我的经验,我希望您确保:

  1. 在发布时,数据是否正确保存到问题实体中。

  2. 如果是这样,在获取映射时,请确保您的子实体使用预加载加载。 (如 efcore.include() 加载子实体)

如果不使用任何外部映射器,请注意:确保添加初始化列表的构造函数。

public class Template
{
    public int Id { get; set; }
    public List<Question> Questions { get; set; }
    public Template()
    {
       Questions = new List<Questions>()
    }
}

请尝试一下,如果您需要我提供的其他信息,请告诉我。

您不是在查询中询问相关的Question实体。

将您的 GET 方法修改为 -

[HttpGet]
public async Task<ActionResult<IEnumerable<Template>>> GetTemplates()
{
    return await _context.Templates.Include(p=> p.Questions).ToListAsync();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM