这是 model 和 数据库上下文
public class Title
{
public int Tid { get; set; }
public string Tname { get; set; }
public int Tnumber { get; set; }
}
public class TitleDbContext : DbContext
{
public DbSet
}
控制器:
namespace Mydesign.Controllers
{
public class TitlesController : Controller
{
private TitleDbContext db = new TitleDbContext();
// GET: Titles
public ActionResult Index()
{
return View(db.Titles.ToList());
}
}
}
以及Index视图
@model IEnumerable
@{
ViewBag.Title = "Index";
}
@Html.DisplayNameFor(model => model.Tname) | @Html.DisplayNameFor(model => model.Tnumber) | |
---|---|---|
@Html.DisplayFor(modelItem => item.Tname) | @Html.DisplayFor(modelItem => item.Tnumber) | @Html.ActionLink("Edit", "Edit", new { id = item.Tid }) | @Html.ActionLink("Details", "Details", new { id = item.Tid }) | @Html.ActionLink("Delete", "Delete", new { id = item.Tid }) |
报错内容这个
发生了 System.Data.Entity.ModelConfiguration.ModelValidationException
HResult=0x80131500
Message=One or more validation errors were detected during model generation:
Mydesign.Models.Title: : EntityType 'Title' has no key defined. Define the key for this EntityType.
Titles: EntityType: EntitySet 'Titles' is based on type 'Title' that has no keys defined.
Source=<无法计算异常源>
StackTrace:
<无法计算异常堆栈跟踪>