控制器:
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);
上面的代码列出了我正在处理的所有类。在运行我的应用程序时,我收到以下错误:
“在模型生成期间检测到一个或多个验证错误”以及“实体类型未定义关键字”。
发布于 2013-12-02 00:36:35
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; }
}
}
发布于 2014-02-28 20:01:38
{get; set;}
(您的是公共变量-一个公共的[Key]
批注,位于您选择的属性之上。发布于 2015-04-03 14:32:13
发生这种情况有几个原因。其中一些是我发现的here,另一些是我自己发现的。
Id
,则需要向其中添加[Key]
属性。uint
、<代码>d13uint>等)不是CLS错误也可能由CLS引起。https://stackoverflow.com/questions/20203492
复制相似问题