ASP.NET MVC 4.0 學習4-Code First

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   檔案   ar   

之前我們需要用到的數據,通過添加Entity實體數據模型把數據庫中需要的Database拉到項目中如下圖,

而就是Code First就是相對於這種處理數據的方法而言的

 Code First更加準確的解讀是開發人員只需要編寫程式(Code Only),系統會自動建立模型和數據庫

我們來建立一個專案看一下Code First的具體實現

1,新專案的Model中加入類別MessageBoard存儲留言資訊

MessageBoard.cs中添加欄位屬性:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;using System.ComponentModel;namespace MvcApplication3.Models{    public class MessageBoard    {        [Key]        public int MsgID { get; set; }        [Required]        [DisplayName("姓名:")]        public string Title { get; set; }        [Required]        [DisplayName("內容")]        public string Content { get; set; }    }}
View Code

2,Ctrl+Shift+B重建方案後,添加MessageController

範本選擇讀寫功能,模型類別選擇剛建立的Model中的MessageBoard

確認以後,我們看到Models和Views中自動產生了對應的檔案,MessageController.cs和View中自動添加了相應增刪改:

Ctrl+F5運行程式,輸入Message如:http://localhost:64570/Message  我們看到了Message的首頁,並且可以添加留言

增刪查改的功能是我們剛才在添加控制器的時候選擇了“具有讀取/寫入...”功能系統自動實現的:

→→

我們剛才並沒有建立數據庫,新增的留言存放在哪裡了?

項目中點擊查看所有檔案,我們發現App_Data下面產生了.mdf檔案,.mdf檔案以及Models下的MvcApplication3Context.cs也是在我們添加MessageController這一步選擇“資料內容類別”的時候產生的:

  →→→→

 雙擊.mdf我們看到了產生的Table和Models下MessageBoard.cs中的屬性是一致的:

 

這就是Code First ,我們只需要去編寫Models中的模型,添加Controller時候系統自動幫我們產生了和模型一致的數據庫檔案

 

接下來我們再看一下Models中的模型:

Code First,Model資料模型中的class對應產生了數據庫中的Table: MessageBoard.cs 對應 MessageBoards表

Model中的Class除了對屬性進行定義外,還能定義Table的名稱,Table中的主鍵以及Table間的一對多 多對多關係:

Model中添加兩個模型類,BookModel.cs和AuthorModel.cs,類中借住System.Collenctons命名空間下的Icollection實現了表與表之間的對應關係:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;namespace MvcApplication3.Models{    //定義表名稱    [Table("MyTable")]    public class BookModels    {        //定義主鍵Key        [Key]        public int id { get; set; }        public string BookName { get; set; }        public DateTime PublishTime { get; set; }        //Book和作者的對應關係: N*1          public AuthorModels AuthorModels { get; set; }    }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.ComponentModel.DataAnnotations;using System.ComponentModel.DataAnnotations.Schema;namespace MvcApplication3.Models{    public class AuthorModels    {        //定義表名稱        [Table("BookAuthor")]        public class Author        {            [Key]            public int id { get; set; }            public string Name { get; set; }            //作者和數的對應關係:一個作者對應多本數 1*多              public ICollection<BookModels> BookModels { get; set; }        }    }}
View Code

 

ASP.NET MVC 4.0 學習4-Code First

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.