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

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

搜索EntityFramework包,点击安装;

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为主键。

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

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个球员的属性;

 插入数据

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,可得刚才添加完的球员信息;

最新文章

  1. mybatis实战教程(mybatis in action)之六:与Spring MVC 的集成
  2. hdu 4039 2011成都赛区网络赛I ***
  3. 当session过期后自动跳转到登陆页而且会跳出iframe框架
  4. 负载均衡 IO etc.
  5. [译] TypeScript入门指南(JavaScript的超集)
  6. [ES6] 13. Using the ES6 spread operator ...
  7. RCP学习笔记
  8. 宣布正式发布 Windows Azure 移动服务、网站及持续的服务创新
  9. 基于python3.x,使用Tornado中的torndb模块操作数据库
  10. node-glob的*匹配
  11. elasticsearch的CPU居高不下的问题
  12. ModelAndView返回json对象的方法
  13. C语言关于进制转换,补码, 整数的位操作
  14. 编译时bad substitution的解决办法
  15. 史上最简洁的UITableView Sections 展示包含NSDicionary 的NSArray
  16. p1474 Money Systems
  17. 《Linux内核分析》 第三周 构造一个简单的Linux系统MenuOS
  18. ctrl+c ctrl+d ctrl+z 的区别和使用场景
  19. json数据进行格式化
  20. 贪心问题 POJ 2393 Yogurt factory

热门文章

  1. android模拟器不能上网设置
  2. 提升HTML5的性能体验系列之一 避免切页白屏
  3. JQuery中after() append() appendTo()的区别
  4. 【Linux】CentOS 7.2 安装 MySQL 5.7.21 解压版
  5. 装箱问题(NOIP2001&水题测试2017082401)
  6. 为什么要选择OMP之合规性
  7. python学习之ansible api
  8. fabric 安装
  9. 社区发现(Community Detection)算法(转)
  10. 使用thymeleaf一旦没有闭合标签就会报错怎么解决