HTML5技术

第二篇 Entity Framework Plus 之 Query Future - 改變世界(2)

字号+ 作者:H5之家 来源:博客园 2016-04-14 16:00 我要评论( )

using System.Data.Entity; using EntityFrameworkPlus.Mappings; using EntityFrameworkPlus.Models; using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.DbContext{ public class EntityFrameworkPlus

using System.Data.Entity; using EntityFrameworkPlus.Mappings; using EntityFrameworkPlus.Models; using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.DbContext { public class EntityFrameworkPlusDbContext : System.Data.Entity.DbContext { public EntityFrameworkPlusDbContext() : ) { } public DbSet<AuditEntry> AuditEntries { get; set; } public DbSet<AuditEntryProperty> AuditEntryProperties { get; set; } public DbSet<OrderModel> Orders { get; set; } public DbSet<GoodsModel> Goodses { get; set; } OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new OrderMap()); modelBuilder.Configurations.Add(new GoodsMap()); base.OnModelCreating(modelBuilder); } } }

3. 右键 “EntityFrameworkPlus.QueryFuture.Demo” 项目,选择“管理NuGet程序包”,关联部分  右上角搜索“Z.EntityFramework.Plus” ,然后选择 “EntityFramework Plus (EF6) | Query Deferred”&“EntityFramework Plus (EF6) | Query Futurn” 两项安装

二. Entity Framework Plus  库 Query Future 扩展功能实作

1. 在 “EntityFrameworkPlus.QueryFuture.Demo” 项目 Program 新增3个静态方法,分别是 

FindOrdersWithGoodsies()  查询订单信息和商品信息

FindPagerOrders(int pageSize, int pageIndex, out int totalCount)  订单分页查询

FindGoodsMaxWithMinUnitPrice() 查询单价最大和最小的商品

详细代码如下

using System.Collections.Generic; using System.Linq; using EntityFrameworkPlus.DbContext; using EntityFrameworkPlus.Models; using Z.EntityFramework.Plus; namespace EntityFrameworkPlus.QueryFuture.Demo { class Program { static void Main(string[] args) { //1.查询订单信息和商品信息 FindOrdersWithGoodsies(); //2. 订单分页查询 //var totalCount = 0; //FindPagerOrders(10, 1, out totalCount); //3.查询单价最大和最小的商品 //FindGoodsMaxWithMinUnitPrice(); } FindOrdersWithGoodsies() { using (var dbContext = new EntityFrameworkPlusDbContext()) { var futureOrders = dbContext.Orders.Future(); var futureGoodsies = dbContext.Goodses.Future(); var orders = futureOrders.ToList(); var goodsies = futureGoodsies.ToList(); } } totalCount) { using (var dbContext = new EntityFrameworkPlusDbContext()) { var orders = dbContext.Orders.OrderBy(o => o.CreateDateTime); var futureCount = orders.DeferredCount().FutureValue(); var futurePagerOrders = orders.Skip((pageIndex - 1) * pageSize).Take(pageSize).Future(); totalCount = futureCount.Value; return futurePagerOrders.ToList(); } } FindGoodsMaxWithMinUnitPrice() { using (var dbContext = new EntityFrameworkPlusDbContext()) { var futureMaxGoodsUnitPrice = dbContext.Goodses.DeferredMax(g => g.UnitPrice).FutureValue<decimal>(); var futureMinGoodsUnitPrice = dbContext.Goodses.DeferredMin(g => g.UnitPrice).FutureValue<decimal>(); var maxGoodsUnitPrice = futureMaxGoodsUnitPrice.Value; var minGoodsUnitPrice = futureMaxGoodsUnitPrice.Value; } } } }

2.  3个方法的SQL追踪和截图

FindOrdersWithGoodsies

., ., ., ., ., ., ., ., ., ..., ., ., ., ., ., ., ., ., ..

FindPagerOrders(int pageSize, int pageIndex, out int totalCount)

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • net.sz.framework 框架 登录服务器架构 单服2 万 TPS(QPS) - 失足程序员

    net.sz.framework 框架 登录服务器架构 单服2 万 TPS(QPS) - 失足

    2017-04-13 11:05

  • Framework7+vue demo - stumpx

    Framework7+vue demo - stumpx

    2017-04-06 13:04

  • EntityFramework Core并发深挖详解,一纸长文,你准备好看完了吗? - Jeffcky

    EntityFramework Core并发深挖详解,一纸长文,你准备好看完了吗? -

    2017-04-05 14:03

  • EntityFramework Core不得不注意的性能优化意外收获,你会用错? - Jeffcky

    EntityFramework Core不得不注意的性能优化意外收获,你会用错? - J

    2017-04-05 14:00

网友点评
.