您的位置:首页 > 编程语言 > ASP

Asp.net MVC4 使用EF实现数据库的增删改查

2013-09-21 15:11 435 查看
EF的使用

步骤:

(1)将EF添加到项目:在Model右击添加新建项

找到ADO.NET实体数据模型,接着。。。

(2)实现数据库的增删改查

查询

(因为在Model中已经添加EF实体了,所以就可以在Controller中进行有关的数据库操作)

<<controller>>

//数据上下文对象

OrderDBEntities db = new OrderDBEntities();

public ActionResult Index() {

//使用SQO(标准查询运算符),查询

//实际返回的是IQueryable 接口的之类对象

//IQueryable<Models.Customer> query = db.Customers.Where(d => d.Address == "111");

//这样转有可能报异常 EnumerableQuery<Models.Customer> query = (EnumerableQuery<Models.Customer>)db.Customers.Where(d => d.Address == "111");

//DbQuery 支持延迟加载,就是只有使用到数据的时候,才去查询数据库

//DbQuery<Models.Customer> query = db.Customers.Where(d => d.Address == "111") as DbQuery<Models.Customer>;

//List<Models.Customer> list = query.ToList();

//也可以这样

// List<Models.Customer> list = db.Customers.Where(d => d.Address == "111").ToList();

//用第二种方式:使用Linq语句,该语法只是给程序员用的,.net编译器会在编译程序集的时候把它转化为SQO

//IQueryable<Models.Customer> query = from d in db.Customers where d.Address == "111" select d;

//List<Models.Customer> list = (from d in db.Customers where d.Address == "111" select d).ToList();

List<Models.Customer> list = (from d in db.Customers select d).ToList();

//使用ViewData将数据传给View

ViewData["DataList"] = list;

return View();

}

<<View>>

<body>

<table border="1">

<!--要取到数据就直接@对应的变量 这样就可以了(导入命名空间也是一个道理)-->

@foreach (Customer a in ViewData["DataList"] as List<Customer>)

{

<tr>

<td>@a.Address</td>

<td>@a.CustomerName</td>

<td><a href="/home/del/@a.CustomerNo">删除</a>

<a href="/home/modify/@a.CustomerNo">修改</a>

</td>

</tr>

}

</table>

</body>



删除:


<<Controller>>

public ActionResult Del(string id) //这个id是通过超链接带过来的

{

try

{

//需要一个实体对象参数

//db.Customers.Remove(new Customer() {CustomerNo = id });

//1,创建要删除的对象

Customer modelDel = new Customer() { CustomerNo = id };

//2,将对象添加到EF管理容器中

db.Customers.Attach(modelDel);

//3,修改对象的包装类对象标识为删除状态

db.Customers.Remove(modelDel);

//4,更新到数据库

db.SaveChanges();

//5,更新成功,则命令流浪器重定向 到 /Home/Index 方法

return RedirectToAction("Index", "Home");

}

catch (Exception )

{

//指定对应跳转的视图Test下的Test.cshtml文件

return RedirectToAction("Test", "Test");

//return Content("删除失败" + ex.Message);

}

}

<<View>>

删除哪有什么视图,成功失败页面不给出了

修改

在视图里面肯定又个修改连接,点击跳转到对应的页面,并将参数传递给对应的函数

<<View>>

<body>

<table border="1">

<!--要取到数据就直接@对应的变量 这样就可以了(导入命名空间也是一个道理)-->

@foreach (Customer a in ViewData["DataList"] as List<Customer>)

{

<tr>

<td>@a.Address</td>

<td>@a.CustomerName</td>

<td><a href="/home/del/@a.CustomerNo">删除</a>

<a href="/home/modify/@a.CustomerNo">修改</a>

</td>

</tr>

}

</table>

</body>

//调用到控制器中的modify方法,并以表单的形式显示相应的页面

<<Controller>>

[HttpGet] //加上这个 只要是超链接发送过来的就调用这个

public ActionResult Modify(string id)

{

Customer art = (from d in db.Customers where d.Address == "111" select d).FirstOrDefault();

//将数据传递给视图:用ViewBag viewData 使用view的构造函数

return View(art);

}

<<View>>

<body>

@using (Html.BeginForm("Modify", "Home", FormMethod.Post))

{

<table>

<tr>

<td colspan="2">修改</td>

</tr>

<tr>

<td>标题:@Html.HiddenFor(a=>a.CustomerNo)</td>

@*<td>@Html.TextBox("textName",(object)Model.CustomerNo)</td>*@

<!--使用htmlHelper的强类型方法,直接从Model中根据CustoomerNo生成文本框-->

<td>@Html.TextBoxFor(a =>a.CustomerNo)</td>

<td>@Html.TextBoxFor(a =>a.CustomerName)</td>

<td><input type="submit" value="确定修改" />@Html.ActionLink("返回","index","home")</td>

</tr>

</table>

}

</body>

当用户点击修改的时候又将数据以Post方式发送给Home控制器中的Modify函数进行处理

<<Controller>>

[HttpPost] //表单提交过来的就调用这个方法

public ActionResult Modify(Customer model)

{

try

{

//1,将实体对象加入EF对象容器中,并获取伪包装类对象

DbEntityEntry<Customer> entry = db.Entry<Customer>(model);

//2,将伪包装类对象的状态设置为unchanged

entry.State = System.Data.EntityState.Unchanged;

//3,设置被改变的属性

entry.Property(a => a.CustomerName).IsModified = true;

//4,提交到数据库 完成修改

db.SaveChanges();

return RedirectToAction("Index", "Home");

}

catch (Exception)

{

return RedirectToAction("Test", "Test");

}

}

补充:MVC中页面之间的跳转是通过MapRouter

代码如下

routes.MapRoute(

name: "Default",

url: "{controller}/{action}/{id}",

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: