当我从 Controller 访问模型的数据时。我收到此错误 EntitySet 'Department' is based on type 'Admin' that has no keys defined。我将 MVC5 与 visual stdio 2013 RC 一起使用。
deptID (Primary key), deptName, Description : 都是Department表的列名。
desgID(主键)、desgName、description:都是Designation表的列名。
目标:- 我想在 View 页面上创建一个 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; }
}
}
最佳答案
Entity Framework 只能确定您希望将哪个属性作为键,如果键被简单命名为 Id
或包含类名,即 AdminId
。它无法知道 deptID
应该是 Admin
类的关键字段。要解决此问题,可以将注释 [Key]
附加到属性。要使用此注解,您需要在您的项目中引用 Entity Framework 并有一个 using 语句 using System.ComponentModel.DataAnnotations
。
关于c# - EntitySet 'Department' 基于没有定义键的类型 'Admin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20805422/