[Asp.net MVC]Asp.net MVC5系列——添加模型
目录
系列文章
[Asp.net MVC]Asp.net MVC5系列——第一个项目
[Asp.net MVC]Asp.net MVC5系列——添加视图
概述
在本节中我们将追加一些类来管理数据库中的学生信息。这些类将成为我们的MVC应用程序中的“模型”部分。
在vs2013中EF的版本为(Entity Framework)EF6,我们将使用EF6来进行对学生信息的维护,顺便也学习一下EF6的增删改查。
添加模型
在解决方案资源管理器中,鼠标右击Models文件夹,点击“添加”菜单下的“新建项”,如图所示。
我这有个学生信息的测试数据库,所以选择了从数据库生成,为了以后方便使用,也懒得去新建数据库了。
选择新建连接
选择“是,在连接字符串中包括敏感数据”,如果选择上面的则连接字符串中密码是以一串“*”替换的,你还得修改,所以怎么简单怎么来了。
选择ef版本,这里选择ef6
选择数据库对象,这里可能要用到student,score,course表,所以将选择三张数据表。
选择数据表后,单击完成,后会弹出一个安全警告的对话框,直接确定忽视它。
之后会弹出三张表的关系图
到目前为止我们创建了三个模型类Student,Score,Course类
具体代码如下:
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码已从模板生成。
//
// 手动更改此文件可能导致应用程序出现意外的行为。
// 如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic; public partial class Student
{
public Student()
{
this.Score = new HashSet<Score>();
} public int stuId { get; set; }
public string stuName { get; set; }
public string stuSex { get; set; }
public System.DateTime stuBirthdate { get; set; }
public System.DateTime stuStudydate { get; set; }
public string stuAddress { get; set; }
public string stuEmail { get; set; }
public string stuPhone { get; set; }
public Nullable<bool> stuIsDel { get; set; }
public Nullable<System.DateTime> stuInputtime { get; set; }
public int classId { get; set; } public virtual Course Course { get; set; }
public virtual ICollection<Score> Score { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码已从模板生成。
//
// 手动更改此文件可能导致应用程序出现意外的行为。
// 如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic; public partial class Course
{
public Course()
{
this.Score = new HashSet<Score>();
this.Student = new HashSet<Student>();
} public int classId { get; set; }
public string className { get; set; }
public string classDescription { get; set; } public virtual ICollection<Score> Score { get; set; }
public virtual ICollection<Student> Student { get; set; }
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// 此代码已从模板生成。
//
// 手动更改此文件可能导致应用程序出现意外的行为。
// 如果重新生成代码,将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------ namespace Wolfy.FirstMVCProject.Models
{
using System;
using System.Collections.Generic; public partial class Score
{
public int testId { get; set; }
public int stuId { get; set; }
public int classId { get; set; }
public int testBase { get; set; }
public int testBeyond { get; set; }
public int testPro { get; set; }
public string testName { get; set; }
public System.DateTime testDate { get; set; } public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
}
在下篇文章中,我们将要创建一个新的SchoolController类,用来显示数据库中的数据,并且允许用户创建学生列表,可以添加学生信息。
总结
在网上看了很多类似的文章都是使用code-first的方式,如果再写一个code-first类似的文章,老花样去玩,没意思,玩玩新的东西还是有必要的。所以既然vs2013中出现了新的玩法,何不尝试一下?
本文关于添加模型的内容较少,大量篇幅说了ef,因为添加模型不知道该说什么?添加一个类,该如何说啊?发愁!
如果你的眼睛比较锋利,也许会发现,从添加ado.net实体模型生成的文件有*.tt的文件,你懂得!这东西也可以研究研究。
参考文章:
http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-a-model
[Asp.net MVC]Asp.net MVC5系列——添加模型的更多相关文章
- ASP.NET MVC 5 学习教程:添加模型
原文 ASP.NET MVC 5 学习教程:添加模型 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 通过控 ...
- 【译】ASP.NET MVC 5 教程 - 4:添加模型
原文:[译]ASP.NET MVC 5 教程 - 4:添加模型 在本节中,我们将添加一些管理电影数据库的类,这些类在ASP.NET MVC 应用程序中扮演“Model”的角色. 我们将使用.NET F ...
- [Asp.net MVC]Asp.net MVC5系列——在模型中添加验证规则
目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...
- [Asp.net MVC]Asp.net MVC5系列——添加数据
目录 概述 显示添加数据时所用表单 处理HTTP-POST 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列 ...
- Asp.net MVC]Asp.net MVC5系列——在模型中添加
目录 概述 在模型中添加验证规则 自定义验证规则 伙伴类的使用 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5 ...
- [Asp.net MVC]Asp.net MVC5系列——从控制器访问模型中的数据
目录 概述 从控制器访问模型中的数据 强类型模型与@model关键字 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net M ...
- [Asp.net MVC]Asp.net MVC5系列——布局视图
目录 系列文章 概述 布局视图 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net MVC5系列——添加视图 [Asp.net M ...
- Asp.net MVC]Asp.net MVC5系列——Routing特性
目录 概述 路由特性 使用路由 可选参数和参数的默认值 路由前缀 默认路由 路由约束 自定义路由约束 路由名 区域(Area) 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列— ...
- Asp.net MVC]Asp.net MVC5系列——实现编辑、删除与明细信息视图
目录 概述 实现信息的明细视图 实现信息的编辑视图 实现信息的删除视图 总结 系列文章 [Asp.net MVC]Asp.net MVC5系列——第一个项目 [Asp.net MVC]Asp.net ...
随机推荐
- cobbler深入学习
cobbler重要目录和cobbler各对象的关系 /var/www/cobbler/ks_mirror 存放操作系统镜像/var/www/cobbler/repo_mirror 存放仓库镜像/var ...
- 记一次DDos攻击--2016/12/8
先上图 图一 图二 午休之后~ 睡意朦胧,报警来了.看到121121Mbps的流量攻击,精神一震. 不到两秒,又来了一个短信,开始心塞.网站入口IP被封了.打开网站,全站瘫痪.紧接着运营就来了,让运营 ...
- lua 闭包
--匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues function newCounter() return function() i = i + r ...
- GLEW OpenGL Access violation when using glGenVertexArrays
http://stackoverflow.com/questions/20766864/glew-opengl-access-violation-when-using-glgenvertexarray ...
- javascript的propertyIsEnumerable()方法使用介绍
hasOwnProperty() 方法用来判断某个对象是否含有指定的自身属性. propertyIsEnumerable()是用来检测属性是否属于某个对象的,如果检测到了,返回true,否则返回fal ...
- yourphp基本语句
实例化页面代码 1.时间代码:{$vo.createtime|toDate=###,'Y-m-d H:i:s'} 2.连接:{:U('Pro/arr')},{:URL()} 如:<form ac ...
- JabRef 文献管理软件
JabRef 文献管理软件简明教程 大多只有使用LaTeX撰写科技论文的研究人员才能完全领略到JabRef的妙不可言,但随着对Word写作平台上BibTeX4Word插件的开发和便利应用,使用Word ...
- spring 容器技术入门
官方文档 翻译 https://waylau.gitbooks.io/spring-framework-4-reference/content/III.%20Core%20Technologies/C ...
- LINUX命令总结 -------来自 水滴娃娃 的CSDN
LINUX命令总结 标签: LINUX命令总结 2014-01-27 15:54 41039人阅读 评论(1) 收藏 举报 分类: linux(1) 版权声明:本文为博主原创文章,未经博主允许不得 ...
- firefox如何卸载插件plugins和临时文件夹
下载原版的 英文版的 firefox 会看到 openH264 video codec Plugin和microsoft DRM (digit rightcopy manager 数字版权管理)等等插 ...