码迷,mamicode.com
首页 > 其他好文 > 详细

ORM开发(4)-分页与配置

时间:2015-06-06 16:30:37      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:

我们接着给页面添加分页,在这里,官方范例给我们安利了一个PagedList.Mvc库,我们只需要在nuget包中就可以安装了。

Install-package PagedList.Mvc

他的用法也挺简单的,首先引入命名空间:

using PagedList;

然后再修改返回值:

int pageSize = 3;
            int pageNumber = (page ?? 1);
            return View(students.ToList().ToPagedList(pageNumber, pageSize));

他会根据页数量和当前页面值决定显示那些数据,同时我们也要修改Model:

@model PagedList.IPagedList<ContosoUniversity.Models.Student>
@using PagedList.Mvc
<link  href="~/Content/PagedList.css" type="text/css" rel="stylesheet"/>

使用方法无需修改,把他当作一个List或IEnuerable接口就行,实际上,他是继承了这个接口。

最后显示分页:

<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

@Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

 

我们在使用云端数据库时有很多好处,通常当数据库连接错误时,我们需要停下来查找错误原因,然后手动修复他,于是用户不得不在那等着数据。这是一种很糟糕的体验,云端数据库会自动帮你处理这些问题,依靠强大的云计算,只要我们再次自动连接他就可以了,EF内置了自动连接,真是连接云端数据库的不二之选。

首先我们需要配置EF,在DAL文件夹中,创建SchoolConfigration:

public class SchoolConfiguration:DbConfiguration
    {
        public SchoolConfiguration()
        {
            SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
        }
    }

程序会在启动时自动初始化继承了DbConfiguration的类。

我们还可以捕获异常,让DbContext在错误时自动连接:

catch (RetryLimitExceededException /* dex */)
{
    //Log the error (uncomment dex variable name and add a line here to write a log.
    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
之后我们还将创建命令拦截,使得我们可以将每次Sql查询记录到日志中。

ORM开发(4)-分页与配置

标签:

原文地址:http://www.cnblogs.com/blackerXHunter/p/4556772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!