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

标签 c# asp.net-mvc model

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/Roll/Create 操作之外的任何操作时,它都会崩溃并显示以下错误消息:

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 项目中,搭建了索引、创建和编辑 View ,并且能够毫无问题地创建和编辑卷。

关于c# - asp.net MVC 4 模型 [Key] 属性无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16005996/

相关文章:

c# - 从流中读取 XML

ASP.NET MVC : StyleBundle IncludeDirectory & CssRewriteUrlTransform

asp.net-mvc - 如何将字符串传递给 ASP.NET MVC 中的分部 View ?

php - Laravel 非静态方法问题

file - 如何使用vcglib保存颜色?

c++ - Qt:将 C++ 中的列表连接到 QML 中的 ListView

c# - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏。在 Windows Phone 8 中

c# - 报告定义无效

c# - MVC 下拉菜单需要 ienumerable?

Jquery DataTable 删除仅适用于第一行