I have a project that has uses a database-first approach. I have used db-scaffold to create the models and have also created 'custom' models to contain extra info and code.
I have a model class VoteQuestion:
using System;
using System.Collections.Generic;
namespace BRPA_CORE_MVC.Models;
public partial class VoteQuestion
{
public int QuestionId { get; set; }
public int MeetingId { get; set; }
public int QuestionSortNumber { get; set; }
public string QuestionText { get; set; } = null!;
public string AnswerType { get; set; } = null!;
public string? IsOpen { get; set; }
}
And an accompanying model class VotePossibleAnswer that can have multiple possible answers to a question:
using System;
using System.Collections.Generic;
namespace BRPA_CORE_MVC.Models;
public partial class VotePossibleAnswer
{
public int AnswerId { get; set; }
public int QuestionId { get; set; }
public string AnswerText { get; set; } = null!;
public string IncludeInResult { get; set; } = null!;
public string IncludeInAttendance { get; set; } = null!;
}
In the custom model for VotePossibleAnswer, I have the following code:
public static List<VotePossibleAnswer> GetAllVotePossibleAnswersListByQuestionID(int QuestionId)
{
using (var ctx = new brpaContext())
{
List<VotePossibleAnswer> answerList = new List<VotePossibleAnswer>();
// answerList = (from a in ctx.VotePossibleAnswers where a.QuestionId == QuestionId select a).OrderBy(a => a.AnswerText).ToList();
// answerList = (from a in ctx.VotePossibleAnswers where a.QuestionId == QuestionId select a).ToList();
// answerList = (from a in ctx.VotePossibleAnswers select a).ToList();
VotePossibleAnswer votePossibleAnswer = (from a in ctx.VotePossibleAnswers select a).FirstOrDefault();
answerList = (from a in ctx.VotePossibleAnswers where a.QuestionId == QuestionId select a).ToList();
return answerList;
}
}
As you can see from the commented out lines, I have tried a few versions...
The solution builds correctly, but when I try to get the PossibleAnswers for a VoteQuestion I get this run-time error:
Microsoft.Data.SqlClient.SqlException: Invalid column name 'VoteQuestionQuestionId'
The dbContext looks like this
modelBuilder.Entity<VotePossibleAnswer>(entity =>
{
entity.HasKey(e => e.AnswerId);
entity.ToTable("VotePossibleAnswer");
entity.Property(e => e.AnswerId).HasColumnName("AnswerID");
entity.Property(e => e.IncludeInAttendance).HasMaxLength(1);
entity.Property(e => e.IncludeInResult).HasMaxLength(1);
entity.Property(e => e.QuestionId).HasColumnName("QuestionID");
});
modelBuilder.Entity<VoteQuestion>(entity =>
{
entity.HasKey(e => e.QuestionId);
entity.ToTable("VoteQuestion");
entity.Property(e => e.QuestionId).HasColumnName("QuestionID");
entity.Property(e => e.AnswerType).HasMaxLength(50);
entity.Property(e => e.IsOpen)
.HasMaxLength(1)
.IsUnicode(false);
entity.Property(e => e.MeetingId).HasColumnName("MeetingID");
});
I have searched for a solution, but cannot find any.
VoteQuestionQuestionId as a text string.
Any suggestions would be most welcome.