如何解决ef core中的“导航属性”问题

时间:2019-09-15 14:01:42

标签: asp.net

我有2个班级的学生和课程。

public class Student
    {
        [Key]
        [HiddenInput]
        public int StudentID { get; set; }

        [Required(ErrorMessage = "Student name is required")]
        [Display(Name = "Student Name")]
        [StringLength(30, ErrorMessage = "Max lenght 30.")]
        [RegularExpression(RegularExpressions.NameSurnameRegEx, ErrorMessage = "Student name is not in valid format")]
        public string StudentName { get; set; }

        [Required(ErrorMessage = "Student Number is required")]
        [Display(Name = "Student Number")]
        [RegularExpression(RegularExpressions.StudentNumberRegEx, ErrorMessage = "Student number is not in valid format")]
        public string StudentNumber { get; set; }

        public int CourseID { get; set; }
        public Course Course { get; set; }
    }
public class Course
    {
        [Key]
        [HiddenInput]
        public int CourseID { get; set; }

        [Required(ErrorMessage = "Course Title is required")]
        [StringLength(30, ErrorMessage = "Max lenght 30.")]
        [Display(Name = "Course Title")]
        public string CourseTitle { get; set; }

        [Required(ErrorMessage = "Course Code is required")]
        [Display(Name = "Course Code")]
        [RegularExpression(RegularExpressions.CourseCodeRegEx, ErrorMessage = "Course number is not in valid format")]
        public string CourseCode { get; set; }

        [Required(ErrorMessage = "Description is required")]
        [StringLength(100, ErrorMessage = "Max lenght 100.")]
        public string CourseDescription { get; set; }

        public List<Student> Students { get; set; }
    }

我的课程列表包含学生的视图模型

public class CourseViewModel
    {
        public IEnumerable<Student> Students { get; set; }
        public Course Course { get; set; }
    }

我必须在课程列表页面中向访问者显示课程标题

<div class="container">
        <h2>@Model.Course.CourseTitle</h2>
        <table class="table table-sm table-bordered">
            <tr>
                <th>#</th>
                <th>Student Name</th>
                <th>Student Number</th>
            </tr>

            @{
                var i = 1;

                @foreach (var student in Model.Course.Students)
                {
                    <tr>
                        <td>@(i++)</td>
                        <td>@student.StudentName</td>
                        <td>@student.StudentNumber</td>
                    </tr>
                }

                <tr>
                    <td colspan="3">Total Student: <strong>@(i - 1)</strong></td>
                </tr>
            }

        </table>

这是我的课程列表控制器


public IActionResult List(int id)
        {

            var course = _context.Courses.Where(x => x.CourseID == id).SingleOrDefault();

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

            _context.Courses.Include(c => c.Students).ToList();

            var viewModel = new CourseViewModel();

            return View(viewModel);
        }

我可以在课程列表中显示学生列表,但不能显示课程标题。 我尝试了很多Include和ThenInclude属性,但我做不到。

我该怎么办?

0 个答案:

没有答案