c# – asp.net MVC 4模型[Key]属性无法识别

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);
        }
    }
}

其中大部分是样板自动生成的代码.每当我点击除了获取/滚动/创建操作之外的任何操作时,它都会出现以下错误消息:

System.Data.Entity.Edm.EdmEntityType: : EntityType ‘Roll’ has no key defined. Define the key for this EntityType.

System.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet ‘Rolls’ is based on type ‘Roll’ that has no keys defined.

但正如您已经看到的那样,有一个关键定义.还有一个为数据库表“Rolls”定义的键支持该模型.我从谷歌得到的每个答案建议添加[Key]注释,我已经有了.

我究竟做错了什么?

最佳答案 将Id更改为int:

public int Id { get; set; }

我将您的代码复制到一个新的MVC项目中,搭建了索引,创建和编辑视图,我能够创建和编辑Rolls,没有任何问题.

点赞