I am new to EF and need help in understanding the following: I have a Class table and a students table in database. One Class can have many students and one student can be in many classes. I have one autogenerated table by EF core called ClassModelStudent. It has following fields:
I have created a razor page to create the class and on post event , the button should send ClassID and redirect to AddStudents page .
Following is the code for AddStudents Class:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using TestProject.Model;
namespace TestProject.Pages.Classes
{
public class EnrollmentModel : PageModel
{
private readonly TestProject.Data.TestProjectContext _context;
public EnrollmentModel(TestProject.Data.TestProjectContext context)
{
_context = context;
}
[BindProperty]
public Enrollment Enrollments { get; set; }
public SelectList Classes { get; set; }
public IList<Student> UserProfile { get; set; }
[BindProperty(SupportsGet = true)]
public string? SearchString { get; set; }
public async Task OnGetAsync()
{
var students = from s in _context.User_Profile
select s;
if (!string.IsNullOrEmpty(SearchString))
{
students = students.Where(s => s.FirstName.Contains(SearchString));
}
UserProfile = await students.ToListAsync();
var classes = from i in _context.Class
orderby i.Description
select i;
Classes = new SelectList(classes, "ClassID",
"Description");
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
//THIS IS WHERE I TRIED ACCESSING CLASSMODELSTUDENT TABLE
//AND THE CONTEXT DID NOT SEE IT
// _context.e
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
}
}
I have currently connected it to Enrollments table. I am not even sure that if it should write it to the autogenerated table ClassModelStudents or manually created Enrollments? Enrollments table looks like the following:
In AddStudents page, I am showing the list of students in a table. I have created 'Add To Class' button in each row of the table. And I want to add that student to the selected class when 'add to class' button is clicked. I should be writing to ClassModelStudents table.
Please see below the screenshot of AddStudents Page:
The thing I am not understanding is how to create the class (Model) for ClassModelStudent? As I cannot access this table directly using context. How to access this table to write the ClassID and StudentID to it?
Please advise.. I would really appreciate your assistance..
following is my model class for ClassModel:
public class ClassModel
{
[Key]
[Required]
[Display(Name ="Class ID")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ClassID { get; set; }
//[ForeignKey("UserProfile")]
//[Display(Name = "User ID")]
//public virtual int ID { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int Occurence { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime Startdate { get; set; }
[Required]
[DataType(DataType.Time)]
public DateTime From { get; set; }
[Required]
[DataType(DataType.Time)]
//[GreaterThanOrEqualTo("From")]
public DateTime To { get; set; }
[Required]
[DataType(DataType.Currency)]
public double Fees { get; set; }
[DisplayFormat(NullDisplayText = "No Instructor Assigned")]
[ForeignKey("InstructorID")]
public virtual int InstructorID { get; set; }
public Instructor? Instructor { get; set; }
[DisplayFormat(NullDisplayText = "No Student Assigned")]
public ICollection<Student>? Students { get; set; }
public ICollection<Enrollment>? Enrollment { get; set; }
}
THIS Is Enrollments class model:
public class Enrollment
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EnrollmentID { get; set; }
[Required]
[ForeignKey("ClassID")]
public virtual int ClassID { get; set; }
[Required]
[ForeignKey("StudentID")]
public virtual int StudentID { get; set; }
[Required]
public ClassModel? Class { get; set; }
[Required]
public Student? Student { get; set; }
}
Below is Student Model:
public class Student
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Display(Name = "StudentID")]
public int StudentID { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public Boolean Status { get; set; }
[DataType(DataType.Date)]
public DateTime DateOfBirth { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[Phone]
[DataType(DataType.PhoneNumber)]
public string PhoneNumber { get; set; }
public string EmergencyContactName { get; set; }
[Phone]
[DataType(DataType.PhoneNumber)]
public string EmergencyContactNumber { get; set; }
[Required]
public string Gender { get; set; }
public string FullName
{
get { return LastName + ", " + FirstName; }
}
[DisplayFormat(NullDisplayText = "No Class Assigned")]
public ICollection<ClassModel>? Classes { get; set; }
public ICollection<Enrollment>? Enrollments { get; set; }
}
I do not have the code for ClassModelStudent as it is autogenerated.