2013-11-25 35 views
100

控制器:的EntityType沒有密鑰定義的錯誤

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using MvcApplication1.Models; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace MvcApplication1.Controllers 
{ 
    public class studentsController : Controller 
    { 
     // 
     // GET: /students/ 

     public ActionResult details() 
     { 
      int id = 16; 
      studentContext std = new studentContext(); 
      student first = std.details.Single(m => m.RollNo == id); 
      return View(first); 
     } 

    } 
} 

的DbContext型號:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 

namespace MvcApplication1.Models 
{ 
    public class studentContext : DbContext 
    { 
     public DbSet<student> details { get; set; } 
    } 
} 

型號:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace MvcApplication1.Models 
{ 
    [Table("studentdetails")] 
    public class student 
    { 
     public int RollNo; 
     public string Name; 
     public string Stream; 
     public string Div; 
    } 
} 

數據庫表:

CREATE TABLE [dbo].[studentdetails](
    [RollNo] [int] NULL, 
    [Name] [nvarchar](50) NULL, 
    [Stream] [nvarchar](50) NULL, 
    [Div] [nvarchar](50) NULL 
) 

在的global.asax.cs

Database.SetInitializer<MvcApplication1.Models.studentContext>(null); 

上面的代碼中列出了所有我工作的類。當運行我的應用我收到錯誤:

"One or more validation errors were detected during model generation" along with "Entity type has no key defined".

+2

檢查存儲庫文件。在這個頁面中的所有解決方案都是驚人的,但在我的情況下,我做了所有事情,但忘記將表格聲明爲DbSet並將其添加到modelbuilder.configurations中。 – Tosh

回答

124

Model類應改爲:

using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.ComponentModel.DataAnnotations; 

namespace MvcApplication1.Models 
{ 
    [Table("studentdetails")] 
    public class student 
    { 
     [Key] 
     public int RollNo { get; set; } 

     public string Name { get; set; } 

     public string Stream { get; set; } 

     public string Div { get; set; } 
    } 
} 
+0

我的代碼工作(沒有'[Key]'),直到我改變數據類型(我將它們更改爲'unsigned'),只有字節的工作原因是我不能使用'unsigned'值? –

+2

實體框架不支持無符號整數http://msdn.microsoft.com/en-us/library/vstudio/bb896317(v=vs.100).aspx#UnsignedIntsUnsupported – Alex

77
  1. 使學生類的確定公共成員被定義爲性能瓦特/ {get; set;} (你的公開是變量 - 一個常見的錯誤)。

  2. [Key]標註放在您選擇的屬性上。

+0

您的答案幫助了我,即使它只是一個我的錯字 –

11

在我的情況下,當使用實體框架創建「帶視圖的MVC 5控制器」時,出現錯誤。

我只需要在創建Model類之後構建項目,並且不需要使用[Key]註釋。

+1

是的 - '接下來,建立項目。 Web API腳手架使用反射來查找模型類,所以它需要編譯的程序集。'取自[本頁](https://docs.microsoft.com/zh-cn/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-2) – Adam

18

我知道這個帖子是遲,但 該解決方案幫助我:

[Key] 
    [Column(Order = 0)] 
    public int RoleId { get; set; } 

添加[列(訂單= 0)] 後[關鍵] 可以通過增量加入按1:

[Key] 
    [Column(Order = 1)] 
    public int RoleIdName { get; set; } 

等等

1

之所以EF框架提示「沒有鑰匙」是EF框架需要在數據庫中的主鍵。要聲明地告訴EF關鍵是哪個屬性,您需要添加一個[Key]註釋。或者,最快的方法是,添加一個ID屬性。然後,默認情況下,EF框架將採用ID作爲主鍵。

+0

發生了什麼?如果數據庫有多個字段後綴ID?在再生時,我失去了Key屬性。 –

+0

如果您從DB生成代碼,則表中的一列應該是表的主鍵。 –

+0

關於「編輯」生成的代碼,它幾乎總是生成爲「partial」類。您可以創建第二個文件作爲具有相同名稱的「partial」類,並使用'[Key]'屬性再次添加該屬性。 –

5

使用[鍵]對我不起作用,但使用id屬性做它。 我只是在我的課堂上添加這個屬性。

public int id {get; set} 
+0

它需要'[Key]',但是一個名爲''id''的屬性也可以工作。 –

55

這可能會發生幾個原因。其中一些我發現了here,其他人是我自己發現的。

  • 如果屬性被命名爲比Id其他的東西,你需要將[Key]屬性添加到它。
  • 關鍵需要是財產,而不是領域。
  • 關鍵需要是public
  • 關鍵需要是一個符合CLS的類型,這意味着無符號類型,如uintulong等是不允許的。
  • 此錯誤也可能由configuration mistakes引起。
+3

我還發現關鍵屬性必須是可讀寫的。當我在ID屬性上有一個getter和setter時,我得到了OP的錯誤。 – JohnFx

+1

@JohnFx絕對正確。在清理過程中,Resharper從一些屬性中刪除了'private set'。結果是「EntityType沒有鍵定義的錯誤」。所以要小心清理工具刪除必要的代碼。 – jeremysawesome

+0

就我而言,我用了一些無關的名字來提及班級的身份。正如您的解決方案中提到的那樣改變了我的問題。謝謝 :) –

0

所有是正確的,但在我的情況我有兩個班這樣

namespace WebAPI.Model 
{ 
    public class ProductsModel 
{ 
     [Table("products")] 
     public class Products 
     { 
      [Key] 
      public int slno { get; set; } 

      public int productId { get; set; } 
      public string ProductName { get; set; } 

      public int Price { get; set; } 
} 
     } 
    } 

刪除它工作正常,我上下課。

相關問題