Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
121 views
in Technique[技术] by (71.8m points)

c# - POST method with List<T> object with EF Core 5.0 API

So I'm new to coding in general and starting to work with .net core 5.0. I'm trying to develop a simple API with Entity Framework Core and I'm having problems when I try to use the POST method. I have a class called Question and another one called Template, which goes as follows

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; }
}

When I go to Postman with my API running, I enter the following into the POST method:

{
    "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  }
    ]
}

But what I get as a response when I call the GET method is something of the sort

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

This is the code part with POST and GET in my TemplatesController class

[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);
    }
}

And here is my DbContext class

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

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

Why do I keep receiving "null" as a response in my "questions" field? Am I missing something, or maybe a concept?

question from:https://stackoverflow.com/questions/65910988/post-method-with-listt-object-with-ef-core-5-0-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are not asking for the related Question entities in your query.

Modify your GET method to -

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...