0

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.

1
  • What happens if you use a consistent naming for your identifiers, such as ID for all of them? Commented Feb 21, 2016 at 20:41

1 Answer 1

3

Your entity class name is Courses .But the primary key column name is CourseID. By convention, It should be either ID or entity class name+ID, which is CoursesID.

Either change your entity class name to Course or change CourseID property to CoursesID

Another option is to decorate your CourseID property with [Key] data annotation.

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

If you do not prefer to use data annotations (the above approach), You can achieve the same thing with fluent api. In your data context class, override the OnModelCreating method and specify which column is the key for the Courses entity class.

public class YourDbContext : DbContext
{ 
  public DbSet<Courses> Courses { set; get; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<Courses>().HasKey(f => f.CourseID);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Keen eye! Of course singular names are recommended.
Thank you man, it worked . I changed from CourseID to CoursesID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.