Invalid column name exception thrown in .NET Core web api

0

I have two classes in my database which are defined as classes, fed into entity and then called from the API

The full method is below for the calls. The first call works fine, the second throws the exception

 public async Task<ActionResult<List<QuizForms>>> GetQuiz([FromQuery]string id)
    {
        var form = await _context.QuizForms.Where(t=>t.QuizId == id).ToListAsync();

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

        var elem = new List<Element>();
        foreach(var e in form)
        {
            var data = await _context.Element.Where(t => t.ElementId == e.ElementId).ToListAsync();
            elem.AddRange(data);
            e.Element.AddRange(elem);
        }

        return form;
    }

When the var data line is hit, an excception is thrown

Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'QuizFormsFormId'.

It looks like the name of the class and column name are being concatenated and the used as the query parameter.

The two classes look like this

public class QuizForms
{
    [Key]
    public int FormId { get; set; }
    public string QuizId { get; set; } = "";
    #nullable enable
    public string? Title { get; set; }
    public int? ElementId { get; set; }
    public List<Element>? Element { get; set; }

    public int? PreviousId { get; set; }
    public int? NextId { get; set; }
    #nullable disable
}

and

public class Element
{
    [Key]
    public int Id { get; set; }
    public int ElementId { get; set; }
    #nullable enable
    public int? MathsId { get; set; }
    public int? QuestionId { get; set; }
    public int? InformationId { get; set; }
    public int? AnswerId { get; set; }
    #nullable disable
    public string QuizId { get; set; } = "";
}

Is it because I'm not using Id for the primary key or do I need to do something else so the class and property aren't concatented like this?

sql-server
asp.net-core
asked on Stack Overflow Jun 15, 2020 by Nodoid

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0