0

I'm try to create web app using asp.net MVC4 I created TeacherController in my controller folder and model also finally i add Db-context also. I'm using sql server.

DbContext like this

using MvcTest2.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace MvcTest5.Models.nuwan600
{
    public class nuwan600:DbContext
    {
        public DbSet<Qualifications> Qualifications { get; set; }
        public DbSet<School> School { get; set; }
        public DbSet<Teacher> Teacher { get; set; }
    }
}

Teacher model

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace MvcTest2.Models
{
    [Table("Teachers")]
    public class Teacher
    {
        [Key]
        public int TeacherID { get; set; }
        public String Name { get; set; }
        public int NIC { get; set; }
        public String Address { get; set; }
        public int Telephone { get; set; }

        public int SchoolID { get; set; }
        public int QualificationID { get; set; }
    }
}

and TeacherController

public ActionResult Index()
{
    int id = 1;
    nuwan600 Teachers = new nuwan600();
    Teacher teachermodle = Teachers.Teacher.Single(emp => emp.TeacherID==id);
    return View(teachermodle);
}

When I run this I got run time error (on this line: Teacher teachermodle = Teachers.Teacher.Single(emp => emp.TeacherID==id); )

One or more validation errors were detected during model generation:
MvcTest5.Models.nuwan600.Qualifications: EntityType 'Qualifications' has no key defined. Define the key for this EntityType.
Qualifications: EntityType: EntitySet 'Qualifications' is based on type 'Qualifications' that has no keys defined.
System.Data.Entity.ModelConfiguration.ModelValidationException

How can fix this ? error need a help.

3
  • where is the error?, also can you show the qualifications class? Commented Apr 13, 2014 at 16:51
  • here i got the error Teacher teachermodle = Teachers.Teacher.Single(emp => emp.TeacherID==id); Commented Apr 13, 2014 at 16:54
  • show your Qualifications class Commented Apr 13, 2014 at 16:55

1 Answer 1

2

The solution is to ... read the error message. ;-)

One or more validation errors were detected during model generation:

MvcTest5.Models.nuwan600.Qualifications: : EntityType 'Qualifications' has no key defined. Define the key for this EntityType. ...

In your Teacher class, you define that TeacherID is a key by using the [Key] attribute. Do the same for your Qualifications class: Mark the (primary) key using the [Key] attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

THe solution is more oto go back to school and learn reading possibly. This is one of the most obvious and expressive error messages, and the poster does not even bother to read it.

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.