【问题标题】:POST method with List<T> object with EF Core 5.0 API带有 EF Core 5.0 API 的 List<T> 对象的 POST 方法
【发布时间】:2021-04-30 19:20:47
【问题描述】:

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

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 的情况下使用 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 类中带有 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 类

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

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

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

【问题讨论】:

  • 你至少要展示你的 API 代码
  • 好的,是哪一部分?
  • 您的操作 [发布] 和 [获取] 返回错误结果
  • 我刚刚在帖子中添加了更多细节
  • 你有一个DbSet&lt;Template&gt; 对应Templates,那么Questions 怎么样?您如何将问题存储在数据库中?

标签: c# asp.net api post entity-framework-core


【解决方案1】:

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

  1. 在帖子中,数据是否正确保存到问题实体中。

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

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

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

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

【讨论】:

    【解决方案2】:

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

    将您的 GET 方法修改为 -

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

    【讨论】:

      猜你喜欢
      • 2021-08-19
      • 1970-01-01
      • 2013-12-07
      • 2021-09-30
      • 2021-12-03
      • 2021-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多