本文主要講的內(nèi)容是 Code First基礎上的MVC 的起步基礎操作。
關于MVC的中心:
- 建立一個Model Class
- 建立一個Scaffolding Item,生成一個MVC 5 View
- View 的模板有Create,Delete,Edit 等等
這次我們的工作是嘗試起步使用一個MVC 例子,使用code first:



稍微熟悉一下 MVC的結構后,我們在Model里面新建一個item
- 命名為contact.cs
- 輸入
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]報錯,自動可以修改,會加入usingSystem.ComponentModel.DataAnnotations.Schema; -
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]這個其實是設置自動增長屬性
利用數(shù)據(jù)生成項 DatabaseGenerated,它后有三個枚舉值: Identity:自增長;None:不處理;Computed:表示這一列是計算列
注意:在EF中,如果主鍵是int類型,Code First生成數(shù)據(jù)庫的時候會自動設置該列為自增長。但如果主鍵是Guid類型,需要手動去設置
DATA Annotations 方式:[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
- 寫入
[Required]這里也會報錯,也是右擊自動修改,在最前面生成了using System.ComponentModel.DataAnnotations; -
[Required] OR [Required(ErrorMessage = "xxx")]只是一個 Data Annotations 方式,表示非空 - 寫入
[DisplayName]報錯右擊自動加入using System.ComponentModel; - 是可以創(chuàng)建自己寫的annotation
public class MustBeTrueAttribute : ValidationAttribute
// MustBeTrueAttribute 是自己創(chuàng)建的,而ValidationAttribute 是.Net 本來就有的Attribute class
{
public override bool IsValid(object value)
{
return value is bool && (bool)value;
}
}
總結
System.ComponentModel.DataAnnotations Attributes
| Attribute | Description |
|---|---|
[DisplayName] |
顯示名字 |
[Required] |
標注這個屬性的字段,不能為空,確保其有數(shù)據(jù) |
[MaxLength] |
給字段設定最大長度 |
System.ComponentModel.DataAnnotations.Schema Attributes:
| Attribute | Description |
|---|---|
[DatabaseGenerated] |
標注了這個特性的屬性,將會映射成數(shù)據(jù)表的計算列字段,所以這個屬性將會是只讀的。它同樣可以用來映射成自動增長列 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace Homework4.Models
{
public class Contact
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[DisplayName("First Name")]
[MaxLength(20, ErrorMessage = "First name must be 20 characters or less.")]
public string FristName { get; set; }
[Required]
[DisplayName("Last Name")]
[MaxLength(20, ErrorMessage = "Last name must be 20 characters or less.")]
public string LastName { get; set; }
[Required]
[DisplayName("Email")]
[DataType(DataType.EmailAddress,ErrorMessage ="Email is not valid.")]
// 這個注釋是允許 yyy@xx.cc這樣的,所以我們選擇自制
[Email]
// 只需要寫Email
[MaxLength(20, ErrorMessage = "Email must be 20 characters or less.")]
public string Email { get; set; }
[DisplayName("Accept Terms and Conditions")]
[MustBeTrue]
public string AcceptTerms { get; set; }
}
public class MustBeTrueAttribute : ValidationAttribute
// MustBeTrueAttribute 是自己創(chuàng)建的,而ValidationAttribute 是.Net 本來就有的Attribute class
{
public override bool IsValid(object value)
{
return value is bool && (bool)value;
}
}
public class EmailAttribute : RegularExpressionAttribute
{
public EmailAttribute()
: base(@"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
{ }
}
}
該段內(nèi)容相關鏈接:
Entity Framwork CodeFirst 學習筆記二:約定和配置
7.DataAnnotations(數(shù)據(jù)注解)【Code-First 系列】
接下來我們嘗試一下做個scaffolding,也就是俗稱的腳手架(下面有圖)

- scaffolding 可以利用model來生成很多不同的內(nèi)容,我們這里嘗試生成一個View
- 首先把Home 里面的Contact.cshtml 刪除掉

- 在做后面步驟之前,首先要先rebuild resolution

- 然后在model里add一個新的 scaffolded item

- 選擇 MVC 5 View

- Create 就是生成新的內(nèi)容
- Model Class 就選擇Contact
- ApplicationDbContext
- 最后還要選擇layout page
- 之后會生成一個 contact.cshtml (滿是紅波浪)
- 之前可能搞錯了,把這個contact.cshtml 放回到View 文件夾里的home文件夾里
- 然后 直接build solution

- 單純是這樣的內(nèi)容,還無法提交,原因是什么?
- 回到controllers文件夾中的HomeController.cs

// 這里只有GET ACTION,沒有SUBMIT ACTION
[HttpPost]
public ActionResult Contact(Contact c)
{
// 檢查modelstate valid,如果不是true,就把用戶重返到當下的頁面,如果是true,就返回thankyouview
if (!ModelState.IsValid)
{
return View(c);
}
return View("ThankYouView", c);
}

- 在view文件夾里的home文件夾中在add 新的thank you view
@{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Thanks for contacting us!</h2>