【问题标题】:EntitySet 'Department' is based on type 'Admin' that has no keys definedEntitySet 'Department' 基于没有定义键的类型 'Admin'
【发布时间】:2013-12-28 02:05:34
【问题描述】:

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

deptID (Primary key), deptName, Description : 都是Department表的列名。

desgID(Primary key)、desgName、description:都是Designation表的列名。

目标:- 我想在视图页面上创建一个 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; }
 }
}

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-28
      • 2021-03-26
      相关资源
      最近更新 更多