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.
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.
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.