2013-04-15 44 views
4
using System; 
using System.ComponentModel.DataAnnotations; 
using System.Data.Entity; 

//my model 
public class Roll 
{ 
    [Key] 
    public uint Id { get; set; } 
    public long RandomSeed { get; set; } 
    public string Expression { get; set; } 
    public DateTime DateCreated { get; set; } 
    public long Total { get; set; } 
} 

//my context 
public class DiceboxContext : DbContext 
{ 
    public DbSet<Roll> Rolls { get; set; } 
} 

//my controller 

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

namespace dicebox.Controllers 
{ 
    public class RollController : Controller 
    { 
     private DiceboxContext db = new DiceboxContext(); 

     // 
     // GET: /Roll/ 

     public ActionResult Index() 
     { 
      return View(db.Rolls.ToList()); 
     } 

     // 
     // GET: /Roll/Details/5 

     public ActionResult Details(int id = 0) 
     { 
      Roll roll = db.Rolls.Find(id); 
      if (roll == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(roll); 
     } 

     // 
     // GET: /Roll/Create 

     public ActionResult Create() 
     { 
      return View(); 
     } 

     // 
     // POST: /Roll/Create 

     [HttpPost] 
     public ActionResult Create(Roll roll) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Rolls.Add(roll); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(roll); 
     } 

     // 
     // GET: /Roll/Edit/5 

     public ActionResult Edit(int id = 0) 
     { 
      Roll roll = db.Rolls.Find(id); 
      if (roll == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(roll); 
     } 

     // 
     // POST: /Roll/Edit/5 

     [HttpPost] 
     public ActionResult Edit(Roll roll) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Entry(roll).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      return View(roll); 
     } 

     // 
     // GET: /Roll/Delete/5 

     public ActionResult Delete(int id = 0) 
     { 
      Roll roll = db.Rolls.Find(id); 
      if (roll == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(roll); 
     } 

     // 
     // POST: /Roll/Delete/5 

     [HttpPost, ActionName("Delete")] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      Roll roll = db.Rolls.Find(id); 
      db.Rolls.Remove(roll); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      db.Dispose(); 
      base.Dispose(disposing); 
     } 
    } 
} 

大部分這是樣板自動生成的代碼。任何時候我打的除了GET /卷的任何行動/創建行動,它吹了以下錯誤消息:asp.net MVC 4 model [Key]屬性不被識別

System.Data.Entity.Edm.EdmEntityType:的EntityType「滾」沒有密鑰定義。定義此EntityType的關鍵字。

System.Data.Entity.Edm.EdmEntitySet:EntityType:EntitySet'Rolls'基於沒有定義鍵的類型'Roll'。

但是,正如您已經看到的那樣,有一個定義的關鍵字。還有一個爲支持該模型的數據庫表「Rolls」定義的鍵。我從谷歌獲得的每個答案都建議添加[關鍵]註釋,而且我已經有了。

我在做什麼錯?

+0

嘗試將'Roll.Id'重命名爲'Roll.RollId'(這是EF的約定,如何獲取關鍵字的命名)並且可能使其成爲int而不是unsigned int?您不需要爲bog標準實體定義進行註釋。 – 48klocs

+0

你似乎大概是一半的權利,因爲如果我使用int vs uint,錯誤是不同的。這是同樣的可怕的設計缺陷,當它是新的時候,它使我從軌道上的紅寶石變成紅色。我的鑰匙的類型和名稱根本不重要。現在我得到「無法檢索'Roll'的元數據,不支持使用相同的DbCompiledModel創建針對不同類型的數據庫服務器的上下文,而是爲每種正在使用的服務器創建一個單獨的DbCompiledModel。」 –

+0

https://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/2085127-support-unsigned-integer-eg-uint-properties-an - uint不支持所有。如果您需要支持負值,則可以通過添加正/負位標記來僞造它。 – 48klocs

回答

2

更改ID爲int:

public int Id { get; set; } 

我複製你的代碼到一個新的MVC項目,搭建的索引,創建和編輯的觀點和我能夠既創建和編輯羅爾斯沒有問題。