在MVC中生成Model时出错

时间:2017-03-08 17:38:59

标签: asp.net-mvc entity-framework

在ASP.Net中生成模型时出现以下错误:

  

MvcApplication1.Models.Department :: EntityType' Department'没有定义键。定义EntityType的密钥。    department:EntityType:EntitySet' department'是基于类型'部门'没有键定义。

这是一个包含更多细节的屏幕截图:

enter image description here

我将主键添加到Department表中:

{    
    [Table("department")]
    public class Department
    {
        public int dept id { get; set; }
        public string department { get; set; }
        public List<Employee> Employees { get; set; }
    }
}

3 个答案:

答案 0 :(得分:0)

您需要在deptid字段中添加[key] annotation

{    
    [Table("department")]
    public class Department
    {
        [Key]
        public int deptid { get; set; }
        public string department { get; set; }
        public List<Employee> Employees { get; set; }
    }
}

答案 1 :(得分:0)

 department table. ' {

[Table("department")]
public class Department
{
   [Key]
    public int dept id { get; set; }
    public string department { get; set; }
    public List<Employee> Employees { get; set; }
}
}`

添加关键字属性,请参阅msdn

答案 2 :(得分:0)

另一种选择是将let students = [['David', 80], ['Vinoth', 77], ['Goren', 55]]; // Define rating function: let rate = (grade) => grade <= 60 ? 'F' : grade <= 70 ? 'D' : grade <= 80 ? 'C' : grade <= 90 ? 'B' : 'A'; // Map students to phrases: let phrases = students.map((student) => `${student[0]}'s grade is ${student[1]} and is ${rate(student[1])} grade.`); // Output DOM nodes: phrases.forEach((phrase) => { let el = document.createElement('div'); el.textContent = phrase; document.body.appendChild(el); });更改为public int deptidpublic int Id。这是一个内置的EF约定,如果您使用此语法,则不需要public int DepartmentId属性。

https://msdn.microsoft.com/en-us/library/jj679962(v=vs.113).aspx#Anchor_1