標籤:des style blog http color 使用 os io
目錄
概述
添加模型
總結
系列文章
[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.Models11 {12 using System;13 using System.Collections.Generic;14 15 public partial class Student16 {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.Models11 {12 using System;13 using System.Collections.Generic;14 15 public partial class Course16 {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.Models11 {12 using System;13 using System.Collections.Generic;14 15 public partial class Score16 {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