c# - EntitySet 'Department' 基于没有定义键的类型 'Admin'

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

当我从 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/

相关文章:

c# - 理解C#的类型转换

C# - Windows 通用应用程序 - 图像源不起作用

git - MS Visual Studio + gitolite 或 github 通过 ssh

c# - Visual Studio 2013 .aspx 智能感知不工作

c# - .NET Core 2.1 中 UserManager 的 AutoSaveChanges

c# - 为什么从我的代码中删除 TextBox.AppendText() 会更改此处的套接字通信?

c# - LINQ to Entities 和空字符串

MySQL 数据库设计问题

c# - asp.net mvc 获取关系数据的更好方法

asp.net - 什么是 Visual Studio 2013 项目使其成为武士刀项目?