久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

.Net下EF的簡(jiǎn)單實(shí)現(xiàn)

 圖書(shū)館煮飯少年 2018-04-12

1.連接SQLServer,,創(chuàng)建數(shù)據(jù)庫(kù)TestDB;

 

2.添加EF引用,,點(diǎn)擊工具-NuGet包管理器-管理解決方案的NuGet程序包,

搜索EntityFramework包,,點(diǎn)擊安裝,;

3.在Web.config中添加節(jié)點(diǎn)

1
2
3
<connectionStrings>
    <add connectionString="Data Source=(local);Initial Catalog=TestDB;Integrated Security=True" name="TestDBDAL" providerName="System.Data.SqlClient" />
  </connectionStrings>

  其中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í)主鍵,;

復(fù)制代碼
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; }
    }
}
復(fù)制代碼

 

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ù);

復(fù)制代碼
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);
        }
    }
}
復(fù)制代碼

 

6.在HomeController.cs下的Index方法中添加獲取Players的方法,;

復(fù)制代碼
  public ActionResult Index()
  {
    TestDBDAL testDBDAL = new TestDBDAL();
    List<PlayerModel> listPlayers = testDBDAL.Players.ToList();
    return View();
  }
復(fù)制代碼

 

7.運(yùn)行項(xiàng)目,;刷新TestDB數(shù)據(jù)庫(kù),會(huì)看到已經(jīng)新建了Players表,,并且有3列屬性,,其中PlayerID為主鍵。

8.為該表添加數(shù)據(jù),,并保存,;

9.在Model文件夾下添加ListPlayerModel.cs新類,并且添加一個(gè)List類型的屬性,;

復(fù)制代碼
using System.Collections.Generic;

namespace WebApplication6.Models
{
    public class ListPlayerModel
    {
        public List<PlayerModel> Employees { get; set; }
    }
}
復(fù)制代碼

10.將HomeController.cs下的Index方法改為獲取數(shù)據(jù)庫(kù)中players,,并且傳遞給頁(yè)面;

復(fù)制代碼
public ActionResult Index()
{
  TestDBDAL testDBDAL = new TestDBDAL();
  ListPlayerModel listPlayerModel = new ListPlayerModel
  {
    Employees = testDBDAL.Players.ToList()
  };
  return View(listPlayerModel);
}
復(fù)制代碼

11.在頁(yè)面構(gòu)造球員列表容器,;

復(fù)制代碼
@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>
復(fù)制代碼

12.運(yùn)行代碼,,頁(yè)面會(huì)出現(xiàn)數(shù)據(jù)庫(kù)中3個(gè)球員的屬性;

 插入數(shù)據(jù)

13.在index.cshtml后追加添加球員的div,;

復(fù)制代碼
<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>
復(fù)制代碼

14.在項(xiàng)目根目錄下添加BAL(業(yè)務(wù)邏輯處理)文件夾,,并在文件夾下新添PlayerBAL.cs類,用來(lái)處理球員相關(guān)的業(yè)務(wù)邏輯,。在該類下添加AddPlayer方法,;

復(fù)制代碼
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;
        }
    }
}
復(fù)制代碼

15.在HomeController下新添AddNewPlayer方法;

復(fù)制代碼
public ActionResult AddNewPlayer(PlayerModel p)
{
    PlayerBAL playerBAL = new PlayerBAL();
    playerBAL.AddPlayer(p);
    return RedirectToAction("Index");
}
復(fù)制代碼

其中RedirectToAction方法是重定向到XXX的方法,,這里是指添加完球員后再次重定向到Index.cshtml這個(gè)View,;

16.運(yùn)行代碼,,并在View上添加球員,點(diǎn)擊Add,,可得剛才添加完的球員信息,;

 

    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多