Cod*_*der 137 .net c# entity-framework
控制器:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
模型:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
数据库表:
CREATE TABLE [dbo].[studentdetails](
[RollNo] [int] NULL,
[Name] [nvarchar](50) NULL,
[Stream] [nvarchar](50) NULL,
[Div] [nvarchar](50) NULL
)
Run Code Online (Sandbox Code Playgroud)
在global.asax.cs中
Database.SetInitializer<MvcApplication1.Models.studentContext>(null);
Run Code Online (Sandbox Code Playgroud)
上面的代码列出了我正在处理的所有类.运行我的应用程序时收到错误:
"在模型生成期间检测到一个或多个验证错误"以及"实体类型没有定义键".
Cod*_*der 158
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; }
}
}
Run Code Online (Sandbox Code Playgroud)
Blu*_*eft 79
这可能有几个原因.其中一些我在这里找到,其他一些是我自己发现的.
Id,则需要向其添加[Key]属性.publicuint,ulong等都是不允许的.dan*_*man 19
我知道这篇文章很晚但是这个解决方案帮助了我:
[Key]
[Column(Order = 0)]
public int RoleId { get; set; }
Run Code Online (Sandbox Code Playgroud)
在[Key]之后添加[Column(Order = 0)]可以按增量加1:
[Key]
[Column(Order = 1)]
public int RoleIdName { get; set; }
Run Code Online (Sandbox Code Playgroud)
等等...
小智 16
在我的情况下,我在使用实体框架创建"带视图的MVC 5控制器"时遇到错误.
我只需要在创建Model类之后构建项目,而不需要使用[Key]注释.
使用[key]对我来说不起作用,但使用id属性就可以了.我只是在我的课堂上添加这个属性.
public int id {get; set;}
Run Code Online (Sandbox Code Playgroud)
该对象必须包含一个将用作 的字段Primary Key,如果您有一个名为的字段,Id则默认情况下这将是实体框架将链接到的对象的主键。
否则,您应该在[Key]要用作 的字段上方添加属性Primary Key,并且您还需要添加命名空间System.ComponentModel.DataAnnotations:
public class myClass
{
public int Id { get; set; }
[Key]
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
另外请记住,不要忘记添加这样的公共关键字
[Key]
int RoleId { get; set; } //wrong method
Run Code Online (Sandbox Code Playgroud)
您必须像这样使用 Public 关键字
[Key]
public int RoleId { get; set; } //correct method
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
251720 次 |
| 最近记录: |