EntitySet'Department'基于类型'Admin',没有定义键

时间:2013-12-27 17:54:25

标签: c# entity-framework visual-studio-2013

当我从控制器访问我的模型数据时。我收到此错误 EntitySet'Department'基于类型'Admin',没有定义键。我正在使用MVC5和visual stdio 2013 RC。

deptID(主键),deptName,描述:all是Department表的列名。

desgID(主键),desgName,description:all是指定表的列名。

目标: - 我想在视图页面上创建一个deptName或desgName对话框,但现在我想解决这些问题。

我指的是这些链接: - EntityType 'Category' has no key defined. Define the key for this EntityType

ASP.NET MVC 3 EntityType has no key defined

但根据这些链接。我正在尝试使用[Key]和我的主键列,但是在mvc5中没有提示。

型号:

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

1 个答案:

答案 0 :(得分:12)

如果密钥只是Id的名称,或者包含类名,即AdminId,则实体框架只能确定您希望将哪个属性作为密钥。它无法知道deptID应该是Admin类的关键字段。要解决此问题,可以将注释[Key]附加到属性。要使用此批注,您需要在项目中引用Entity Framework并使用using语句using System.ComponentModel.DataAnnotations