.Net下EF的简单实现

时间:2021-11-10 00:22:17

1.连接SQLServer,创建数据库TestDB;

.Net下EF的简单实现

2.添加EF引用,点击工具-NuGet包管理器-管理解决方案的NuGet程序包,

.Net下EF的简单实现

搜索EntityFramework包,点击安装;

.Net下EF的简单实现

3.在Web.config中添加节点

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

  其中Data Source为服务器名,Initial Catalog为刚才在SQLServer中新建的数据库名,name则是接下来在代码中会使用到的名字,数据访问层和数据库之间的映射通过名称实现的,ConnectionString(连接字符串)的名称和数据访问层的类名称是相同的,都是TestDBDAL,因此会自动实现映射;

4.在Models文件下添加“PlayerModel”新类,为该类添加三个属性,并引用System.ComponentModel.DataAnnotations命名空间,在PlayerID属性上加上[Key]关键字标识主键;

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.在项目下添加“DataAccessLayer”文件夹,并且添加“TestDBDAL.cs”新类,并且引用System.Data.Entity命名空间,使该类继承DbContext类。定义映射关系,重写OnModelCreating方法,其中Players为表名,运行时会自动生成在SQLServer中。再在该类下定义一个DbSet类型的新属性,表示数据库中能查询到的所有play数据;

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.运行项目;刷新TestDB数据库,会看到已经新建了Players表,并且有3列属性,其中PlayerID为主键。

.Net下EF的简单实现

8.为该表添加数据,并保存;

.Net下EF的简单实现

9.在Model文件夹下添加ListPlayerModel.cs新类,并且添加一个List类型的属性;

using System.Collections.Generic;

namespace WebApplication6.Models
{
public class ListPlayerModel
{
public List<PlayerModel> Employees { get; set; }
}
}

10.将HomeController.cs下的Index方法改为获取数据库中players,并且传递给页面;

public ActionResult Index()
{
  TestDBDAL testDBDAL = new TestDBDAL();
  ListPlayerModel listPlayerModel = new ListPlayerModel
  {
    Employees = testDBDAL.Players.ToList()
  };
  return View(listPlayerModel);
}

11.在页面构造球员列表容器;

@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.运行代码,页面会出现数据库中3个球员的属性;

.Net下EF的简单实现

 插入数据

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.在项目根目录下添加BAL(业务逻辑处理)文件夹,并在文件夹下新添PlayerBAL.cs类,用来处理球员相关的业务逻辑。在该类下添加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这个View;

16.运行代码,并在View上添加球员,点击Add,可得刚才添加完的球员信息;

.Net下EF的简单实现