目錄
概述
添加模型
總結
系列文章
[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/
部落格版權:如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起讨論,共同進步!
再次感謝您耐心的讀完本篇文章。