• 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 = 0; 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);
            }
        }
    }
  • 相关阅读:
    Firefly官方教程之DBentrust使用文档
    《烽烟OL》简介
    《暗黑世界》简介
    CrossApp简介
    内联元素于与块元素的转换 相对定位、绝对定位以及fixed定位 Z轴覆盖
    div介绍 盒子模型边框属性 CSS初始化 文字排版 边框调整 溢出
    CSS样式补充第二天
    CSS样式
    form表单以及内嵌框架标签
    html对a标签的运用以及属性,img图像标签的属性及应用
  • 原文地址:https://www.cnblogs.com/liuxiaoji/p/11727380.html
Copyright © 2020-2023  润新知