EF4.4增删改查实例
第一、先创建一个名为Store数据库,将下面脚本代码执行创建表;
USE [Store]
GO /****** Object: Table [dbo].[Category] Script Date: 03/25/2014 09:39:23 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Category](
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED
(
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO /****** Object: Table [dbo].[Product] Script Date: 03/25/2014 09:39:31 ******/
SET ANSI_NULLS ON
GO SET QUOTED_IDENTIFIER ON
GO CREATE TABLE [dbo].[Product](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[CategoryID] [int] NULL,
[UnitPrice] [decimal](18, 2) NULL,
[UnitsInStock] [int] NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO 第二、控制器ProductController
//
// GET: /Product/ public ActionResult Index()
{
var products = db.Products;
return View(products.ToList());
} /// <summary>
// GET: /Products/Details/5
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Details(int id = )
{
var product = db.Products.First(p => p.ID == id);
if (product == null)
return HttpNotFound();
return View(product);
} //
// GET: /Products/Create
public ActionResult Create()
{
ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name");
return View();
} //
// POST: /Products/Create [HttpPost]
public ActionResult Create(Product product)
{
if (ModelState.IsValid)
{
db.AddToProducts(product);
db.SaveChanges();
return RedirectToAction("Index");
} ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name", product.CategoryID);
return View(product);
} //
//GET: /Products/Edit/5
public ActionResult Edit(int id = )
{
Product entity = db.Products.First(p => p.ID == id);
if (entity == null)
return HttpNotFound();
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", entity.CategoryID);
return View(entity);
} //
//POST: /Products/Edit/5
[HttpPost]
public ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.Categories.ToList(), "CategoryID", "Name", product.CategoryID);
return View(product);
} //
// GET: /Products/Delete/5 public ActionResult Delete(int id = )
{
Product product = db.Products.First(p => p.ID == id);
if (product == null)
{
return HttpNotFound();
}
return View(product);
} //
// POST: /Products/Delete/5 [HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Product product = db.Products.First(p => p.ID == id);
db.CreateObjectSet<Product>().Attach(product);
db.ObjectStateManager.ChangeObjectState(product, EntityState.Deleted);
db.SaveChanges();
return RedirectToAction("Index");
} 第3、视图
视图结构:
下面是视图代码:
Index:
@model IEnumerable<TMVC.DAL.Product>
@{
ViewBag.Title = "Index";
}
<h2>
产品页面</h2>
<p>@Html.ActionLink("添加", "Create")</p>
<table>
<tr>
@*<th>@Html.DisplayNameFor(model => model.Name)
</th>
<th>@Html.DisplayNameFor(model => model.CategoryID)
</th>
<th>@Html.DisplayNameFor(model => model.UnitPrice)
</th>
<th>@Html.DisplayNameFor(model => model.UnitsInStock)
</th>*@
<th>
名称
</th>
<th>
类别
</th>
<th>
价格
</th>
<th>
库存
</th>
<th>
编辑
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(mi => item.Name)
</td>
<td>@Html.DisplayFor(mi => item.CategoryID)
</td>
<td>@Html.DisplayFor(mi => item.UnitPrice)
</td>
<td>@Html.DisplayFor(mi => item.UnitsInStock)
</td>
<td>@Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
@Html.ActionLink("详细", "Details", new { id = item.ID }) | @Html.ActionLink("删除", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
Create:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Create";
}
<h2>
添加</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true);
<fieldset>
<legend>产品</legend>
<div class="editor-label">
名称 @* @Html.LabelFor(model => model.Name)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别 @* @Html.LabelFor(model => model.CategoryID, "CategoryPCategor")*@
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格 @*@Html.LabelFor(model => model.UnitPrice)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存@* @Html.LabelFor(model => model.UnitsInStock)*@
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="确定添加" />
</p>
</fieldset>
}
Delete:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Delete";
}
<h2>
删除</h2>
<h3>
你确定要删除这个产品吗?</h3>
<fieldset>
<legend>产品</legend>
<div class="display-label">
名称
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
类别
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Name)
</div>
<div class="display-label">
价格
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitPrice)
</div>
<div class="display-label">
库存
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UnitsInStock)
</div>
</fieldset>
@using (Html.BeginForm())
{
<p>
<input type="submit" value="删除" />
|
@Html.ActionLink("返回产品列表", "Index")
</p>
}
Details:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Details";
}
<h2>
详细</h2>
<fieldset>
<legend>产品</legend>
<p>
名称:@Html.DisplayFor(model => model.Name)</p>
<p>
类别:@Html.DisplayFor(model => model.CategoryID)</p>
<p>
价格:@Html.DisplayFor(model => model.UnitPrice)</p>
<p>
库存:@Html.DisplayFor(model => model.UnitsInStock)</p>
</fieldset>
<p>@Html.ActionLink("编辑", "Edit", new { id = Model.ID }) | @Html.ActionLink("返回", "Index")</p>
Edit:
@model TMVC.DAL.Product
@{
ViewBag.Title = "Edit";
}
<h2>
编辑</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true) <fieldset>
<legend>产品</legend>
@Html.HiddenFor(model => model.ID)
<div class="editor-label">
名称
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
类别
</div>
<div class="editor-field">
@*@Html.DropDownList("CategoryID", String.Empty)*@
@Html.EditorFor(model => model.CategoryID)
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
<div class="editor-label">
价格
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitPrice)
@Html.ValidationMessageFor(model => model.UnitPrice)
</div>
<div class="editor-label">
库存
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UnitsInStock)
@Html.ValidationMessageFor(model => model.UnitsInStock)
</div>
<p>
<input type="submit" value="保存" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("返回产品列表", "Index")
</div>
RouteConfig
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
EF4.4增删改查实例的更多相关文章
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)
1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- yii2.0增删改查实例讲解
yii2.0增删改查实例讲解一.创建数据库文件. 创建表 CREATE TABLE `resource` ( `id` int(10) NOT NULL AUTO_INCREMENT, `textur ...
- 百度鹰眼Java接口调用增删改查实例
因感觉百度鹰眼的使用场景比较符合实际业务,于是对百度鹰眼做了简单功能调试.刚开始使用springframework封装的RestTemplate,但是测试提示ak参数不存在.后又试了几种方法,均提示a ...
- 【php增删改查实例】第四节 -自己 DIY 一个数据库管理工具
本节介绍如何自己DIY一个数据库管理工具,可以在页面输入sql 进行简单的增删改查操作. 首先,找到xampp的安装目录,打开htdocs: 新建一个php文件,名称为 mysqladmin.php ...
- Maven多模块项目+MVC框架+AJAX技术+layui分页对数据库增删改查实例
昨天刚入门Maven多模块项目,所以简单写了一个小测试,就是对数据库单表的增删改查,例子比较综合,写得哪里不妥还望大神赐教,感谢! 首先看一下项目结构: 可以看到,一个项目MavenEmployee里 ...
- 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作
PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...
- 【php增删改查实例】第十节 - 部门管理模块(新增功能)
正常情况下,在一个部门管理页面,不仅仅需要展示列表数据,还需要基本的增删改操作,所以,我们先把之前写好的新增功能集成进来. 在toolbar中,添加一个新增按钮. <div id="t ...
- Java MVC 增删改查 实例
需求:实现增加新部门的功能,对应数据库表示Oracle的dept表 一.Java MVC 增 实现: 1.视图层(V):注册部门 deptAdd.jsp 在注册新部门页面只需输入“部门名称”和“城市” ...
随机推荐
- Javascript几个时髦的hack技巧《Javascript Hacks for Hipsters》
转自:http://berzniz.com/post/68001735765/javascript-hacks-for-hipsters Javascript Hacks for Hipsters J ...
- 三.jenkins 在windows上配置master 和 agent(slave)
参考链接: https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+mach ...
- js自执行函数、调用递归函数、圆括号运算符、函数声明的提升
前言 起因是我要在jquery的ajax中需要根据返回值来决定是否继续发起ajax请求,这是一个有条件的循环,符合条件就跳出.可以使用while循环的,但是想了想还是递归调用好用. 调用递归函数 递归 ...
- Volo.Abp.EntityFrameworkCore.MySQL 使用
创建新项目 打开 https://cn.abp.io/Templates ,任意选择一个项目类型,然后创建项目,我这里创建了一个Web Api 解压项目,还原Nuget,项目目录如下: 首先我们来查看 ...
- WebBrowser控件支持WebSocket
修改html页面,在Header标签中添加如下标签: <meta http-equiv="X-UA-Compatible"content="IE=edge" ...
- [mvc]记一次“项目”的历程
大二上半学期因为选修课的原因,答应帮老师完善学院的选课系统.在这之前没有做过一个可以成为“项目”的项目,本着挑战自己的原则和可以不上选修课的福利,断断续续用了一学期的时间来完善这个选课系统. 接受这个 ...
- python网络编程--socket,网络协议,TCP
一. 客户端/服务端架构(用到网络通信的地方) 我们使用qq.微信和别人聊天,通过浏览器来浏览页面.看京东的网站,通过优酷.快播(此处只是怀念一下)看片片啥的等等,通过无线打印机来打印一个word文档 ...
- “全栈2019”Java异常第十二章:catch与异常匹配
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- logstash5.5.0同步sql server数据
注意:jdbc.conf和jdbc.sql文件编码都为ANSI jdbc.conf内容如下: input { stdin { } jdbc { jdbc_connection_string => ...
- windows10 pip install MySQL-python mysqlclient
https://dev.mysql.com/downloads/connector/python/ 到上述地址下载对应系统的驱动程序安装即可. 安装mysqlclient方法如下: https://w ...
