目录
概述
添加模型
总结
系列文章
[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类
具体代码如下:
1 //------------------------------------------------------------------------------
2 // <auto-generated>
3 // 此代码已从模板生成。
4 //
5 // 手动更改此文件可能导致应用程序出现意外的行为。
6 // 如果重新生成代码,将覆盖对此文件的手动更改。
7 // </auto-generated>
8 //------------------------------------------------------------------------------
9
10 namespace Wolfy.FirstMVCProject.Models
11 {
12 using System;
13 using System.Collections.Generic;
14
15 public partial class Student
16 {
17 public Student()
18 {
19 this.Score = new HashSet<Score>();
20 }
21
22 public int stuId { get; set; }
23 public string stuName { get; set; }
24 public string stuSex { get; set; }
25 public System.DateTime stuBirthdate { get; set; }
26 public System.DateTime stuStudydate { get; set; }
27 public string stuAddress { get; set; }
28 public string stuEmail { get; set; }
29 public string stuPhone { get; set; }
30 public Nullable<bool> stuIsDel { get; set; }
31 public Nullable<System.DateTime> stuInputtime { get; set; }
32 public int classId { get; set; }
33
34 public virtual Course Course { get; set; }
35 public virtual ICollection<Score> Score { get; set; }
36 }
37 }
1 //------------------------------------------------------------------------------
2 // <auto-generated>
3 // 此代码已从模板生成。
4 //
5 // 手动更改此文件可能导致应用程序出现意外的行为。
6 // 如果重新生成代码,将覆盖对此文件的手动更改。
7 // </auto-generated>
8 //------------------------------------------------------------------------------
9
10 namespace Wolfy.FirstMVCProject.Models
11 {
12 using System;
13 using System.Collections.Generic;
14
15 public partial class Course
16 {
17 public Course()
18 {
19 this.Score = new HashSet<Score>();
20 this.Student = new HashSet<Student>();
21 }
22
23 public int classId { get; set; }
24 public string className { get; set; }
25 public string classDescription { get; set; }
26
27 public virtual ICollection<Score> Score { get; set; }
28 public virtual ICollection<Student> Student { get; set; }
29 }
30 }
1 //------------------------------------------------------------------------------
2 // <auto-generated>
3 // 此代码已从模板生成。
4 //
5 // 手动更改此文件可能导致应用程序出现意外的行为。
6 // 如果重新生成代码,将覆盖对此文件的手动更改。
7 // </auto-generated>
8 //------------------------------------------------------------------------------
9
10 namespace Wolfy.FirstMVCProject.Models
11 {
12 using System;
13 using System.Collections.Generic;
14
15 public partial class Score
16 {
17 public int testId { get; set; }
18 public int stuId { get; set; }
19 public int classId { get; set; }
20 public int testBase { get; set; }
21 public int testBeyond { get; set; }
22 public int testPro { get; set; }
23 public string testName { get; set; }
24 public System.DateTime testDate { get; set; }
25
26 public virtual Course Course { get; set; }
27 public virtual Student Student { get; set; }
28 }
29 }
在下篇文章中,我们将要创建一个新的SchoolController类,用来显示数据库中的数据,并且允许用户创建学生列表,可以添加学生信息。
在网上看了很多类似的文章都是使用code-first的方式,如果再写一个code-first类似的文章,老花样去玩,没意思,玩玩新的东西还是有必要的。所以既然vs2013中出现了新的玩法,何不尝试一下?
本文关于添加模型的内容较少,大量篇幅说了ef,因为添加模型不知道该说什么?添加一个类,该如何说啊?发愁!
如果你的眼睛比较锋利,也许会发现,从添加ado.net实体模型生成的文件有*.tt的文件,你懂得!这东西也可以研究研究。
参考文章:
http://www.asp.net/mvc/tutorials/mvc-5/introduction/adding-a-model
-
博客地址:http://www.cnblogs.com/wolf-sun/
博客版权:如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。