1.連接SQLServer,,創(chuàng)建數(shù)據(jù)庫(kù)TestDB;
2.添加EF引用,,點(diǎn)擊工具-NuGet包管理器-管理解決方案的NuGet程序包, 搜索EntityFramework包,,點(diǎn)擊安裝,; 3.在Web.config中添加節(jié)點(diǎn)
其中Data Source為服務(wù)器名,,Initial Catalog為剛才在SQLServer中新建的數(shù)據(jù)庫(kù)名,,name則是接下來(lái)在代碼中會(huì)使用到的名字,數(shù)據(jù)訪問(wèn)層和數(shù)據(jù)庫(kù)之間的映射通過(guò)名稱實(shí)現(xiàn)的,ConnectionString(連接字符串)的名稱和數(shù)據(jù)訪問(wèn)層的類名稱是相同的,,都是TestDBDAL,,因此會(huì)自動(dòng)實(shí)現(xiàn)映射; 4.在Models文件下添加“PlayerModel”新類,,為該類添加三個(gè)屬性,,并引用System.ComponentModel.DataAnnotations命名空間,在PlayerID屬性上加上[Key]關(guān)鍵字標(biāo)識(shí)主鍵,; using System.ComponentModel.DataAnnotations; namespace WebApplication6.Models { public class PlayerModel { [Key] public int PlayerID { get; set; } public string EnglishName { get; set; } public string ChineseName { get; set; } } }
5.在項(xiàng)目下添加“DataAccessLayer”文件夾,并且添加“TestDBDAL.cs”新類,,并且引用System.Data.Entity命名空間,,使該類繼承DbContext類。定義映射關(guān)系,,重寫(xiě)OnModelCreating方法,,其中Players為表名,運(yùn)行時(shí)會(huì)自動(dòng)生成在SQLServer中,。再在該類下定義一個(gè)DbSet類型的新屬性,,表示數(shù)據(jù)庫(kù)中能查詢到的所有play數(shù)據(jù); using System.Data.Entity; using WebApplication6.Models; namespace WebApplication6.DataAccessLayer { public class TestDBDAL : DbContext { public DbSet<PlayerModel> Players { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PlayerModel>().ToTable("Players"); base.OnModelCreating(modelBuilder); } } }
6.在HomeController.cs下的Index方法中添加獲取Players的方法,; public ActionResult Index() { TestDBDAL testDBDAL = new TestDBDAL(); List<PlayerModel> listPlayers = testDBDAL.Players.ToList(); return View(); }
7.運(yùn)行項(xiàng)目,;刷新TestDB數(shù)據(jù)庫(kù),會(huì)看到已經(jīng)新建了Players表,,并且有3列屬性,,其中PlayerID為主鍵。 8.為該表添加數(shù)據(jù),,并保存,; 9.在Model文件夾下添加ListPlayerModel.cs新類,并且添加一個(gè)List類型的屬性,; using System.Collections.Generic; namespace WebApplication6.Models { public class ListPlayerModel { public List<PlayerModel> Employees { get; set; } } } 10.將HomeController.cs下的Index方法改為獲取數(shù)據(jù)庫(kù)中players,,并且傳遞給頁(yè)面; public ActionResult Index() { TestDBDAL testDBDAL = new TestDBDAL(); ListPlayerModel listPlayerModel = new ListPlayerModel { Employees = testDBDAL.Players.ToList() }; return View(listPlayerModel); } 11.在頁(yè)面構(gòu)造球員列表容器,; @using WebApplication6.Models; @model ListPlayerModel @{ Layout = null; } <div> <table> <tr> <th>EnglishName</th> <th>ChineseName</th> </tr> @foreach (PlayerModel player in Model.Employees) { <tr> <td>@player.EnglishName</td> <td>@player.ChineseName</td> </tr> } </table> </div> 12.運(yùn)行代碼,,頁(yè)面會(huì)出現(xiàn)數(shù)據(jù)庫(kù)中3個(gè)球員的屬性; 插入數(shù)據(jù) 13.在index.cshtml后追加添加球員的div,; <div> Add New Player<br /> <form action="/Home/AddNewPlayer" method="post"> EnglishName:<input name="EnglishName" value="" type="text" /><br /> ChineseName:<input name="ChineseName" value="" type="text" /><br /> <input type="submit" value="Add" /> </form> </div> 14.在項(xiàng)目根目錄下添加BAL(業(yè)務(wù)邏輯處理)文件夾,,并在文件夾下新添PlayerBAL.cs類,用來(lái)處理球員相關(guān)的業(yè)務(wù)邏輯,。在該類下添加AddPlayer方法,; using WebApplication6.DataAccessLayer; using WebApplication6.Models; namespace WebApplication6.BAL { public class PlayerBAL { public PlayerModel AddPlayer(PlayerModel player) { TestDBDAL testDBDAL = new TestDBDAL(); testDBDAL.Players.Add(player); testDBDAL.SaveChanges(); return player; } } } 15.在HomeController下新添AddNewPlayer方法; public ActionResult AddNewPlayer(PlayerModel p) { PlayerBAL playerBAL = new PlayerBAL(); playerBAL.AddPlayer(p); return RedirectToAction("Index"); } 其中RedirectToAction方法是重定向到XXX的方法,,這里是指添加完球員后再次重定向到Index.cshtml這個(gè)View,; 16.運(yùn)行代碼,,并在View上添加球員,點(diǎn)擊Add,,可得剛才添加完的球員信息,;
|
|
來(lái)自: 圖書(shū)館煮飯少年 > 《.NET》