Student class :
public class Students
{
public int ID { get; set; }
public string Fname { get; set; }
public string Lname { get; set; }
public DateTime EnrollmentDate { get; set; }
//on one to many relationship Student can have many enrollments so its a collection of Enrollments
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Enrollment :
public enum Grade
{
A, B, C, D, F
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
//? will take the default value, to avoid null expections as object value not set, if the grade not above theen also passes with out any errors.
public Grade? Grade { get; set; }
//single enrollment has single course , single we give the Courses as Class name
public virtual Courses Course { get; set; }
//single enrollment has single student, single we give the Student as Class name
public virtual Students Student { get; set; }
}
Courses class :
public class Courses
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
// A course has many enrollments
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Controller - getting error at
db.Students.Add(objstu)
when I'm running the application for the first time and wanted to see autogenerated tables. But while its connecting to database am getting this error
public ActionResult CreateStudent(Students objstu)
{
if (!ModelState.IsValid)
{
return View(objstu);
}
db.Students.Add(objstu);
return View();
}
Error details :
One or more validation errors were detected during model generation:
DAL.Courses: : EntityType 'Courses' has no key defined. Define the key for this EntityType.
Courses: EntityType: EntitySet 'Courses' is based on type 'Courses' that has no keys defined.
IDfor all of them?