Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntitySet 'Department' is based on type 'Admin' that has no keys defined

When I am accessing my Model's Data from a Controller. I got this error EntitySet 'Department' is based on type 'Admin' that has no keys defined. I am using MVC5 with visual stdio 2013 RC.

deptID (Primary key), deptName, Description : all are column name of Department table.

desgID (Primary key), desgName, description: all are column name of Designation table.

Objective :- I want to create a dialog box of deptName or desgName on the view page but now I want to resolve these issue.

I am referring these link :- EntityType 'Category' has no key defined. Define the key for this EntityType

ASP.NET MVC 3 EntityType has no key defined

but according these link. I am trying to use [Key] with my primary key column but it's not prompt in mvc5.

Model :

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

namespace mvcAppraisalSystem.Models
{
  public class Admin
 {
    public int deptID { get; set; }
    public string deptName { get; set; }
    public string Description { get; set; }
    public int desgID { get; set; }
    public string desgName { get; set; }
    public string description { get; set; }
}

 public class AdminDBContext : DbContext
 {
    public DbSet<Admin> Department { get; set; }

    public DbSet<Admin> Designation { get; set; }
 }
}
like image 611
Anurag Jain Avatar asked Oct 25 '25 06:10

Anurag Jain


1 Answers

Entity Framework can only determine which property you wish to have as the key if the key is named simply Id or includes the class name, i.e. AdminId. It has no way of knowing that deptID should be the key field for an Admin Class. To fix this, there is an annotation [Key] that can be attached to a property. To use this annotation, you need to reference Entity Framework in your project and have a using statement using System.ComponentModel.DataAnnotations.

like image 187
Claies Avatar answered Oct 26 '25 19:10

Claies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!