StudentSystem - Unable to determine the relationship represented by navigation property
Здравейте.
Получавам следната грешка при опит на домашното за Entity Relations
System.InvalidOperationException: 'Unable to determine the relationship represented by navigation property 'Course.StudentsEnrolled' of type 'ICollection<Student>'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore'.
Явно нещо някъде съм объркал и не мога да намеря какво. Вече няколко пъти го преглеждам...
Ако някой хване грешката ще съм благодарен.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace P01_StudentSystem.Data.Models
{
public class Student
{
public Student()
{
this.CourseEnrollments = new HashSet<Course>();
this.HomeworkSubmissions = new HashSet<Homework>();
}
public int StudentId { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Column(TypeName = "char(10)")]
public string PhoneNumber { get; set; }
public DateTime RegisteredOn { get; set; }
public DateTime? Birthday { get; set; }
public ICollection<Course> CourseEnrollments { get; set; }
public ICollection<Homework> HomeworkSubmissions { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace P01_StudentSystem.Data.Models
{
public class Course
{
public Course()
{
this.StudentsEnrolled = new HashSet<Student>();
this.Resources = new HashSet<Resource>();
this.HomeworkSubmissions = new HashSet<Homework>();
}
public int CourseId { get; set; }
[Required]
[MaxLength(180)]
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Price { get; set; }
public ICollection<Student> StudentsEnrolled { get; set; }
public ICollection<Resource> Resources { get; set; }
public ICollection<Homework> HomeworkSubmissions { get; set; }
}
}
namespace P01_StudentSystem.Data.Models
{
public class StudentCourse
{
public int CourseId { get; set; }
public Course Course { get; set; }
public int StudentId { get; set; }
public Student Student { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using P01_StudentSystem.Data.Models;
namespace P01_StudentSystem.Data
{
public partial class StudentSystemContext : DbContext
{
public StudentSystemContext()
{
}
public StudentSystemContext(DbContextOptions<StudentSystemContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Resource> Resources { get; set; }
public DbSet<Homework> Homeworks { get; set; }
public DbSet<StudentCourse> StudentsCourses { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=P01_StudentSystem;Integrated Security=True");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<StudentCourse>(x =>
{
x.HasKey(x => new { x.CourseId, x.StudentId });
});
base.OnModelCreating(modelBuilder);
}
}
}