上篇我们说到了EFCore的基础使用,这篇我们将讲解下基于EFCore的扩展.

我们在Mango.Framework.EFCore类库项目中创建一个类名EFExtended的扩展类,并且引入相关的命名空间

using System;
using System.Reflection;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Linq.Expressions;
namespace Mango.Framework.EFCore
{
    public static class EFExtended
    {}
}

第一个扩展:基于传统DataTable(这个在2.0中才开始有)的返回

        #region 查询返回DataTable的扩展

        //自定义SQL语句             public static DataTable QueryDataTable(this DbContext context, string sql, params SqlParameter[] parameters)
        {
            DbConnection connection = null;
            DbCommand command = null;
            try
            {
                connection = context.Database.GetDbConnection();
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                }
                command = connection.CreateCommand();
                command.CommandText = sql;
                command.Parameters.AddRange(parameters);
                DbDataReader reader = command.ExecuteReader();
                var result= FillDataTable(reader);
                //释放连接资源
                command.Dispose();
                connection.Close();
                //
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //释放连接资源
                if (command != null)
                {
                    command.Dispose();
                }
                if (connection != null)
                {
                    connection.Close();
                }
            }
        }
        private static DataTable FillDataTable(DbDataReader reader)
        {
            bool defined = false;

            DataTable table = new DataTable();

            while (reader.Read())
            {
                object[] values = new object[reader.FieldCount];
                //插入列信息
                if (!defined)
                {
                    ; i < reader.FieldCount; i++)
                    {
                        DataColumn column = new DataColumn()
                        {
                            ColumnName = reader.GetName(i),
                            DataType = reader.GetFieldType(i)
                        };

                        table.Columns.Add(column);
                    }

                    defined = true;
                }

                //插入数据
                reader.GetValues(values);
                DataRow dataRow = table.NewRow();
                ; i < values.Length; i++)
                {
                    dataRow[i] = values[i];
                }
                table.Rows.Add(dataRow);
            }
            return table;
        }
        #endregion

就是通过DbCommand类执行SQL语句返回DbDataReader的数据集合,将返回的数据集合填充到DataTable实例中去.

第二个扩展:基于SQL语句的查询返回指定的集合对象

using System;
using System.Collections.Generic;
using System.Reflection;
using System.ComponentModel;
namespace Mango.Framework.EFCore
{
    public class DbIdentity
    {
        internal static object Change(object value, Type type)
        {
            if (type.IsGenericParameter)
            {
                if (type.IsGenericParameter && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                {
                    if (value == null)
                    {
                        return null;
                    }
                    NullableConverter nullableConverter = new NullableConverter(type);
                    value = Convert.ChangeType(value, type);
                }
            }
            return value;
        }
    }

}

使用Asp.Net Core MVC 开发项目实践[第三篇:基于EF Core的扩展]的更多相关文章

  1. 使用Asp.Net Core MVC 开发项目实践[第四篇:基于EF Core的扩展2]

    上篇我们说到了基于EFCore的基础扩展,这篇我们讲解下基于实体结合拉姆达表达式的自定义更新以及删除数据. 先说下原理:其实通过实体以及拉姆达表达式生成SQL语句去执行 第一种更新扩展: 自定义更新字 ...

  2. 使用Asp.Net Core MVC 开发项目实践[第五篇:缓存的使用]

    项目中我们常常会碰到一些数据,需要高频率用到但是又不会频繁变动的这类,我们就可以使用缓存把这些数据缓存起来(比如说本项目的导航数据,帖子频道数据). 我们项目中常用到有Asp.Net Core 本身提 ...

  3. 使用Asp.Net Core MVC 开发项目实践[第一篇:项目结构说明]

    先从下图看整体项目结构: Mango.Manager: 为后台管理项目 Mango.Web: 为前台项目 Mango.Framework.Core: 为常用的基础操作类项目 Mango.Framewo ...

  4. 使用Asp.Net Core MVC 开发项目实践[第二篇:EF Core]

    在项目中使用EF Core还是比较容易的,在这里我们使用的版本是EF Core 2.2. 1.使用nuget获取EF Core包 这个示例项目使用的是SQLSERVER,所以还需要下载Microsof ...

  5. ASP.NET自定义控件组件开发 第一章 第三篇

    原文:ASP.NET自定义控件组件开发 第一章 第三篇 第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待 ...

  6. ASP.NET自定义控件组件开发 第一章 第三篇 第一章的完结篇

    ASP.NET自定义控件组件开发 第一章 第三篇   第三篇:第一章的完结篇 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ...

  7. 《ASP.NET Core应用开发入门教程》与《ASP.NET Core 应用开发项目实战》正式出版

    “全书之写印,实系初稿.有时公私琐务猬集,每写一句,三搁其笔:有时兴会淋漓,走笔疾书,絮絮不休:有时意趣萧索,执笔木坐,草草而止.每写一段,自助覆阅,辄摇其首,觉有大不妥者,即贴补重书,故剪刀浆糊乃不 ...

  8. ASP.NET Core Web开发学习笔记-1介绍篇

    ASP.NET Core Web开发学习笔记-1介绍篇 给大家说声报歉,从2012年个人情感破裂的那一天,本人的51CTO,CnBlogs,Csdn,QQ,Weboo就再也没有更新过.踏实的生活(曾辞 ...

  9. Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录

    1.登录的实现 登录功能实现起来有哪些常用的方式,大家首先想到的肯定是cookie或session或cookie+session,当然还有其他模式,今天主要探讨一下在Asp.net core 2.0下 ...

随机推荐

  1. boost asio 学习(三)post与dispatch

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio?pg=4 本章节为io_ ...

  2. 在centos7上使用最简单的方法把php脚本做成服务,随开机启动运行

    1.准备文件:coffeetest.service # copy to /usr/lib/systemd/system # systemctl enable coffeetest.service [U ...

  3. Linux学习--- C语言关键字、数据类型

    关键字: sizeof为关键字,并不是函数. 作用:编译器给我们查看内存空间容量的一个工具. eg:int a: printf("the size is %d\n",sizeof ...

  4. python模块:configparser

    """Configuration file parser. A configuration file consists of sections, lead by a &q ...

  5. javascript的常用事件

  6. AngularJS封装webupload实现文件夹上传

    百度的webupload没有开放api获取整个文件夹的信息.本文是二次开发webupload实现获取文件夹信息. 指令封装 /** * @license lx.ui.framework v1.0.0 ...

  7. php中 curl, fsockopen ,file_get_contents 三个函数

    赵永斌:有些时候用file_get_contents()调用外部文件,容易超时报错.换成curl后就可以.具体原因不清楚curl 效率比file_get_contents()和fsockopen()高 ...

  8. 消息中间件——activeMQ

    Activemq使用教程 解压activmq进入bin\win64 启动activemq.bat 启动成功 浏览器访问http://127.0.0.1:8161 创建maven工程 在pom.xml中 ...

  9. MySQL 基础--字符类型

    ##=====================================================================================## MySQL支持的字符 ...

  10. 机器学习&深度学习之路

    计划最近好好按步骤按阶段系统性的学习下机器学习和深度学习,希望能坚持下去. 2019-01-05 基于TensorFlow的深度学习系列教程 2--常量Constant 2019-01-03 深度学习 ...