EF 查询扩展
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection; namespace Holder.ERP.System.Services
{
/// <summary>
/// EF扩展
/// </summary>
public static class EFExtension
{/// <summary>
/// 返回查询的数据
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="context">数据上下文</param>
/// <param name="sql">sql语句</param>
/// <param name="parameters">slq参数</param>
/// <returns></returns>
public static async Task<IEnumerable<T>> ExecuteReaderAsync<T>(this DbContext context, RawSqlString sql, params object[] parameters) where T : new()
{
var relationalDataReader = await ExecuteReaderAsync(context.Database, sql, parameters.ToArray());
var reader = relationalDataReader.DbDataReader;
var properties = typeof(T).GetProperties();
var result = new List<T>();
while (reader.Read())
{
var row = new Dictionary<string, object>();
for (var fieldCount = ; fieldCount < reader.FieldCount; fieldCount++)
{
row.Add(reader.GetName(fieldCount), reader[fieldCount]);
}
var data = new T();
foreach (var prop in properties)
{
if (row.TryGetValue(prop.Name, out object value))
{
prop.SetValue(data, value == DBNull.Value ? null : value);
}
}
result.Add(data);
}
reader.Close();
return result;
} public static async Task<RelationalDataReader> ExecuteReaderAsync(
this DatabaseFacade databaseFacade,
RawSqlString sql,
IEnumerable<object> parameters,
CancellationToken cancellationToken = default(CancellationToken))
{
using (await databaseFacade.GetService<IConcurrencyDetector>().EnterCriticalSectionAsync(cancellationToken))
{
RawSqlCommand rawSqlCommand = databaseFacade.GetRelationalService<IRawSqlCommandBuilder>().Build(sql.Format, parameters);
return await rawSqlCommand.RelationalCommand.ExecuteReaderAsync(databaseFacade.GetRelationalService<IRelationalConnection>(), rawSqlCommand.ParameterValues, cancellationToken);
}
} private static TService GetRelationalService<TService>(
this IInfrastructure<IServiceProvider> databaseFacade)
{
TService service = databaseFacade.Instance.GetService<TService>();
if (service != null)
return service;
throw new InvalidOperationException(RelationalStrings.RelationalNotInUse);
}
}
}
EF 查询扩展的更多相关文章
- linq和EF查询的用法和区分
我们做项目时,难免会遇到用的不知道是啥,及把linq和EF搞混了 今天我带领大家梳理下思路: 首先说linq查询,然后介绍EF查询 1.linq查询 当我们使用linq查询时,转到定义会调到Query ...
- EF查询百万级数据的性能测试--多表连接复杂查询
相关文章:EF查询百万级数据的性能测试--单表查询 一.起因 上次做的是EF百万级数据的单表查询,总结了一下,在200w以下的数据量的情况(Sql Server 2012),EF是可以使用,但是由于 ...
- sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)
sql的行转列(PIVOT)与列转行(UNPIVOT) 在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...
- EF查询视图只得到一条记录
1.出错结果:数据库表视图有多条数据,在使用EF框架进行查询时却只得到一条数据(注:拦截EF得到的sql语句在数据库进行查询并没有任务问题). 2.出错原因:该视图中没有ID或者主键,EF查询时进行反 ...
- EF查询生成的SQL
在EF 4和EF 3.5 SP1中,我们可以使用ToTraceString()方法得到EF查询所生成的SQL. using (var context = new TestDBEntities()) { ...
- EF查询数据库框架的搭建
一个简单的EF查询框架除了运行项目外,大概需要5个类库项目,当然这个不是一定要这样做,这可以根据自己的需要设置有多少个项目.这里介绍的方法步骤只适合EF零基础的人看看就是了. 在开始之前,先建立一个运 ...
- Dapper 链式查询 扩展
Dapper 链式查询扩展 DapperSqlMaker Github地址:https://github.com/mumumutou/DapperSqlMaker 欢迎大佬加入 Demo: 查询 ...
- EF 查询视图出现重复数据
解决方案: 由多张表组成的视图,要加实体键.而且实体键组合要能确保唯一性. 个人理解:确保唯一性,一个或多个实体键,实现了复合主键或组合主键的效果. 这样查询是,延迟加载机制,才知道哪些需要重新从数据 ...
- asp.net core系列 32 EF查询数据 必备知识(1)
一.查询的工作原理 Entity Framework Core 使用语言集成查询 (LINQ) 来查询数据库中的数据. 通过 LINQ 可使用 C#(或你选择的其他 .NET 语言)基于派生上下文和实 ...
随机推荐
- ci 框架中defined('BASEPATH') OR exit('No direct script access allowed');
作用: OR 就是前面的是true时,就不走后面了. 加这个是为了防止不是从index.php访问到的控制器
- C++ UFT-8和GB2312间的转换
在这个帖子找到的代码 还蛮好用的 https://bbs.csdn.net/topics/391040755 #include <codecvt> #include <locale& ...
- sqlserver创建链接服务器连接sqlserver脚本
示例: EXEC sp_addlinkedserver @server='MyLinkServer', --链接服务器别名 @srvproduct='', @provider='SQLOLEDB', ...
- [面试] Java高级软件工程师面试考纲(转)
如果要应聘高级开发工程师职务,仅仅懂得Java的基础知识是远远不够的,还必须懂得常用数据结构.算法.网络.操作系统等知识.因此本文不会讲解具体的技术,笔者综合自己应聘各大公司的经历,整理了一份大公司对 ...
- LC 593. Valid Square
Given the coordinates of four points in 2D space, return whether the four points could construct a s ...
- 动态绑定v-model
<template> <div class="pieAll" > <template v-for="(item, index) in tes ...
- handler方法
post(Runnable) postAtTime(Runnable,long) postDelayed(Runnable long) post类方法允许你排列一个Runnable对象到主线程队列中 ...
- RabbitMQ 使用参考
http://www.zouyesheng.com/rabbitmq.html 安装 基本概念 基本形式 持久化 调度策略 5.1. fanout 5.2. direct 5.3. topic 5.4 ...
- avast关闭DeepScreen
在“设置”-“防病毒”-去掉“启用 DeepScreen”.
- 树莓派使用c语言控制管脚--wiringPi安装
树莓派先安装git,然后安装库 命令如下 git clone https://github.com/WiringPi/WiringPi cd wiringPi ./build 测试--输出管脚信息 g ...