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)