赞
踩
首先安装SqlSever 2008(版本可自己选择),安装教程在网上很多,不赘述。
1、安装完成之后,在开始程序菜单找到SSMS(Sql Sever Managerment Studio),双击进入,截图如下:
到此SQLServer就已经搭建完成,下面就需要创建Model,并快速生成对Model也就是表的增删改查(Model的含义看你自己理解)。
EF的使用
步骤:
(1)将EF添加到项目:在Models右击添加新建项
找到ADO.NET实体数据模型,接着添加....
(2)实现数据库的增删改查
DBContext:
- // <auto-generated>
- // 此代码已从模板生成。
- //
- // 手动更改此文件可能导致应用程序出现意外的行为。
- // 如果重新生成代码,将覆盖对此文件的手动更改。
- // </auto-generated>
- //------------------------------------------------------------------------------
-
- namespace WebApplication9.Models
- {
- using System;
- using System.Data.Entity;
- using System.Data.Entity.Infrastructure;
-
- public partial class NewUserTestEntities : DbContext
- {
- public NewUserTestEntities()
- : base("name=NewUserTestEntities")
- {
- }
-
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- throw new UnintentionalCodeFirstException();
- }
-
- public virtual DbSet<Users> Users { get; set; }//对表的操作需要用到Users对象
- }
- }
UsersModel:
- //------------------------------------------------------------------------------
- // <auto-generated>
- // 此代码已从模板生成。
- //
- // 手动更改此文件可能导致应用程序出现意外的行为。
- // 如果重新生成代码,将覆盖对此文件的手动更改。
- // </auto-generated>
- //------------------------------------------------------------------------------
-
- namespace WebApplication9.Models
- {
- using System;
- using System.Collections.Generic;
-
- public partial class Users
- {
- public string UserName { get; set; }
- public string Password { get; set; }
- public Nullable<int> age { get; set; }
- }
- }
UsersController.cs
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Entity;
- using System.Linq;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- using WebApplication9.Models;
-
- namespace WebApplication9.Controllers
- {
- public class UsersController : Controller
- {
- private NewUserTestEntities db = new NewUserTestEntities();
-
- // GET: /Users/
- public ActionResult Index()
- {
- return View(db.Users.ToList());
- }
-
- // GET: /Users/Details/5
- public ActionResult Details(string id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Users users = db.Users.Find(id);
- if (users == null)
- {
- return HttpNotFound();
- }
- return View(users);
- }
-
- // GET: /Users/Create
- public ActionResult Create()
- {
- return View();
- }
-
- // POST: /Users/Create
- // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
- // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Create([Bind(Include="UserName,Password,age")] Users users)
- {
- if (ModelState.IsValid)
- {
- db.Users.Add(users);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
-
- return View(users);
- }
-
- // GET: /Users/Edit/5
- public ActionResult Edit(string id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Users users = db.Users.Find(id);
- if (users == null)
- {
- return HttpNotFound();
- }
- return View(users);
- }
-
- // POST: /Users/Edit/5
- // 为了防止“过多发布”攻击,请启用要绑定到的特定属性,有关
- // 详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult Edit([Bind(Include="UserName,Password,age")] Users users)
- {
- if (ModelState.IsValid)
- {
- db.Entry(users).State = EntityState.Modified;
- db.SaveChanges();
- return RedirectToAction("Index");
- }
- return View(users);
- }
-
- // GET: /Users/Delete/5
- public ActionResult Delete(string id)
- {
- if (id == null)
- {
- return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
- }
- Users users = db.Users.Find(id);
- if (users == null)
- {
- return HttpNotFound();
- }
- return View(users);
- }
-
- // POST: /Users/Delete/5
- [HttpPost, ActionName("Delete")]
- [ValidateAntiForgeryToken]
- public ActionResult DeleteConfirmed(string id)
- {
- Users users = db.Users.Find(id);
- db.Users.Remove(users);
- db.SaveChanges();
- return RedirectToAction("Index");
- }
-
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- db.Dispose();
- }
- base.Dispose(disposing);
- }
- }
- }
View:Index.cshtml
- @model IEnumerable<WebApplication9.Models.Users>
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
- <p>
- @Html.ActionLink("Create New", "Create")
- </p>
- <table class="table">
- <tr>
- <th>
- @Html.DisplayNameFor(model => model.UserName)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.Password)
- </th>
- <th>
- @Html.DisplayNameFor(model => model.age)
- </th>
- <th></th>
- </tr>
-
- @foreach (var item in Model) {
- <tr>
- <td>
- @Html.DisplayFor(modelItem => item.UserName)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.Password)
- </td>
- <td>
- @Html.DisplayFor(modelItem => item.age)
- </td>
- <td>
- @Html.ActionLink("Edit", "Edit", new { id=item.UserName }) |
- @Html.ActionLink("Details", "Details", new { id=item.UserName }) |
- @Html.ActionLink("Delete", "Delete", new { id=item.UserName })
- </td>
- </tr>
- }
-
- </table>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。