MVC5+EF6实现增删改查

首先在项目工程里面添加NuGet包;EntityFramework、EntityFramework.SqlServerCompact

在Models里面添加类(和数据库表字段,类型匹配)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace mvcuser.Models
{
    public class Userinfo
    {
        [System.ComponentModel.DataAnnotations.Key]  /*注释userid是主键*/
        public int userid { get; set; }
        public string name { get; set; }
        public string password { get; set; }
        public string sex { get; set; }
    }
}

然后再添加一个上下文文件用来关联数据库UsrerContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using mvcuser.Models;

namespace mvcuser.Models
{
    public class UsrerContext:DbContext
    {
        public UsrerContext() : base("name=SQLstring")
        {

        }
        public DbSet<Userinfo> Userinfo { get; set; }//关联数据表
        //DbSet是一个模版类,<>中代表的是模版类中的对象类型, TEntity是一个类,可以是自定义的,也可以是系统中已经定义好的类

        //DBContext类重写方法:
        //EF默认会在数据库里生成一个重复的表(es、s)Entity Framework 映射的时候默认给数据库表名添加复数“s”或者“es”
        //modelBuilder.Conventions.Remove更改避免生成重复
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

}

然后就是新建一个控制器UserController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mvcuser.Models;
namespace mvcuser.Controllers
{
    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            UsrerContext uc = new UsrerContext();
            return View(uc.Userinfo.ToList());
        }
    }
}

在UserController控制器中添加Index视图

Index视图

扫描二维码关注公众号,回复: 4902424 查看本文章
@model IEnumerable<mvcuser.Models.Userinfo>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@*查询*@
<form action="~/User/searcher" method="post">
    名称:<input type="text" name="name" />
       <select name="sex">
           <option value="男"></option>
           <option value="女"></option>
       </select>
    <input type="submit" value="查询" />
</form>

<table class="table">
    <tr>
        <th>
            名称
            @*@Html.DisplayNameFor(model => model.name)*@
        </th>
        <th>
            密码
            @*@Html.DisplayNameFor(model => model.password)*@
        </th>
        <th>
            性别
            @*@Html.DisplayNameFor(model => model.sex)*@
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.password)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.sex)
        </td>
        <td>
            @Html.ActionLink("编辑", "Edit", new { id=item.userid }) |
            @Html.ActionLink("查看", "Details", new { id=item.userid }) |
            @Html.ActionLink("删除", "Delete", new { id=item.userid })
        </td>
    </tr>
}

</table>

成功显示了,所有的数据

然后在UserController添加功能需求,增删查改;添加对应的视图

    public class UserController : Controller
    {
        // GET: User
        public ActionResult Index()
        {
            UsrerContext uc = new UsrerContext();
            return View(uc.Userinfo.ToList());
        }
        //查看
        public ActionResult Details(int id)
        {
            UsrerContext User = new UsrerContext();
            var userDetails = User.Userinfo.Where(x => x.userid == id);
            return View(userDetails.ToList()[0]);
        }
        //数据添加
        public ActionResult Create()
        {
            return View();
        }
        public ActionResult Add(Userinfo user)
        {
            UsrerContext US = new UsrerContext();
            US.Userinfo.Add(user);
            US.SaveChanges();//更新保存
            return View("Index", US.Userinfo.ToList());
        }
        //编辑
        public ActionResult Edit(int id)
        {
            UsrerContext Edit = new UsrerContext();
            var user = Edit.Userinfo.Where(x => x.userid == id);
            return View(user.ToList()[0]);
        }
        public ActionResult update(Userinfo user)
        {
            UsrerContext upda = new UsrerContext();
            //查询数据
            var userUP = upda.Userinfo.Where(x => x.userid == user.userid);
            //新数据覆盖旧数据
            userUP.ToList()[0].name = user.name;
            userUP.ToList()[0].password = user.password;
            userUP.ToList()[0].sex = user.sex;
            upda.SaveChanges();//进行保存更新
            return View("Index", upda.Userinfo.ToList());
        }

        //删除
        public ActionResult Delete(int id)
        {
            UsrerContext Det = new UsrerContext();
            var user = Det.Userinfo.Where(x => x.userid == id);
            Det.Userinfo.Remove(user.ToList()[0]);
            Det.SaveChanges();
            return View("Index", Det.Userinfo.ToList());
        }

        //查询
        [HttpPost]
        public ActionResult searcher(string name, string sex)
        {
            UsrerContext sear = new UsrerContext();
            //var user = sear.Userinfo.Where(x => x.name == name);
            var user = sear.Userinfo.Where(x => x.name.Contains(name)).Where(x => x.sex.Contains(sex));//Contains包含查询
            return View("Index", user.ToList());
        }

    }

对应的视图

猜你喜欢

转载自www.cnblogs.com/live8/p/10264078.html