简体   繁体   中英

Error occurring in Entity Framework in ASP.NET MVC while connecting to database

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.

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);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM