EF框架的查询和分页查询

什么是EF框架:

EF框架是微软的。NET中ORM(对象关系映射)框架。

为什么要用EF框架:

让开发人员不必讲精力放在管理数据访问http://ADO.NET代码上。
EF框架如何运作:将实体类映射到数据库模式-》将linq查询翻译并执行到sql-》跟踪实体再其生命周期内发生的变化,并将更改保存到数据库。

总结:

EF很简单啦,就是简化数据库操作嘛。EF框架作为一个中转站 桥梁,将实体类和数据模型进行连接,贯通

示例

下面展示一些 内联代码片

视图页面代码
@{
    ViewBag.Title = "Index";
}

<h2> 666</h2>
<div class="input-group">
    <label>名称:</label>
    <input type="text" class="form-control" id="txtCondName" value="@ViewBag.name"/>
    <input type="button" id="btnSearch" value="搜索" onclick="page(1);" />
</div>
<table class="table table-bordered">
    <thead>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>备注</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <th>@item.ID</th>
                <th>@item.Name</th>
                <th>@item.Remark</th>
            </tr>
        }
    </tbody>

</table>


<nav aria-label="...">
    <ul class="pagination">
        <li class="page-item ">
            共 @ViewBag.totalPages 页,第
            <input type="number" min="1" max="@ViewBag.totalPages" id="pageIndex" value="@ViewBag.pageIndex" />
            页,每页显示
          
            <select id="pageSize" onchange="page(1);">
                @{
                    var listPageSize = new List<int> { 5, 10, 20, 50, 100 };
                }
                @foreach (var item in listPageSize)
                {
                    if (@ViewBag.pageSize == @item)
                    {
                        <option value="@item" selected="selected">@item</option>
                    }
                    else
                    {
                        <option value="@item">@item</option>
                    }
                }
            </select></li>
    </ul>
    <ul class="pagination">
        <li class="page-item ">
            <a class="page-link" href="javascript:page(1);" tabindex="-1" aria-disabled="true">首页</a>
        </li>
        @if (@ViewBag.pageIndex > 1)
        {
            <li class="page-item"><a class="page-link" href="javascript:page(@ViewBag.pageIndex-1);">上页</a></li>
        }
        else
        {
            <li class="page-item disabled"><a class="page-link" href="javascript:page(@ViewBag.pageIndex-1);">上页</a></li>
        }


        <li class="page-item " aria-current="page">
            <span class="page-link"> @ViewBag.pageIndex</span>
        </li>

        @if (@ViewBag.pageIndex < @ViewBag.totalPages)
        {
            <li class="page-item " aria-current="page">
                <a class="page-link" href="javascript:page(@ViewBag.pageIndex+1);">下页 </a>
            </li>
        }
        else
        {
            <li class="page-item disabled" aria-current="page">
                <a class="page-link" href="javascript:page(@ViewBag.pageIndex+1);">下页 </a>
            </li>
        }

        <li class="page-item">
            <a class="page-link" href="javascript:page(@ViewBag.totalPages);">末页</a>
        </li>
        <li class="page-item">
            <input type="button" class="page-link" value="GO" onclick="go()" />
        </li>
    </ul>
</nav>

@section scripts{

    <script>
        function page(pageIndex) {
            var name = $("#txtCondName").val(); //获取搜索框的值
            var pageSize = $("#pageSize").val(); //获取下拉框的值

            window.location.href = "/role/index?pageindex=" + pageIndex
                + "&pageSize=" + pageSize + "&name=" + name;

        }

        function go() {
            //获取输入的页码的值
            var pageIndex = $("#pageIndex").val();
            page(pageIndex);

        }


    </script>

}
控制器代码
public ActionResult Index(int pageIndex=1,int pageSize=10,string name="")
        {
            //获取总记录数
            var totalRows = db.Roles.Count();
            //计算总页数
            var totalPages = Math.Ceiling(totalRows * 1.00 / pageSize);
            ViewBag.totalPages = totalPages;

            var roles = db.Roles.Where(p=>p.Name.Contains(name))
                .OrderBy(p=>p.ID)  //排序
                .Skip((pageIndex-1) * pageSize)    //跳过
                .Take(pageSize)   //取
                .ToList();
            ViewBag.pageIndex = pageIndex;
            ViewBag.name = name;
            ViewBag.pageSize = pageSize;
            return View(roles);
        }

你可能感兴趣的:(EF框架的查询和分页查询)