上篇我们说到了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. nginx配置备忘

    一.本地测试环境配置 upstream gongsibao{ server ; server ; #fair; } server { listen ; server_name ubuntu00.xus ...

  2. windows 性能监视器常用计数器

    转载地址:https://www.jianshu.com/p/f4406c29542a?utm_campaign=maleskine&utm_content=note&utm_medi ...

  3. Effective C++ 笔记:条款 32 确定你的public继承塑造出正确的is-a关系

    32 : Make sure public inheritance models "is-a." 0 引言 Inheritance and Object-Oriented Desi ...

  4. 别人的Linux私房菜(4)安装CentOS7

    linux磁盘分区参考: 添加磁盘分区(总30G). BIOS boot 2MB 系统自定义文件系统 分区格式为主要分区 /boot 1GB  文件系统为xfs  主要分区 / 10GB 文件系统为x ...

  5. day30

    作业 #__author : 'liuyang' #date : 2019/4/11 0011 下午 12:36 # 这两天 1.软件开发规范 不写 没法做新作业 #2. 认证+上传 +下载 + 校验 ...

  6. MFC在对话框中嵌入对话框

    在对话框中嵌入子对话框 代码 m_childDlg = new CChildDlg(); m_childDlg->Create(IDD_CHILD_DIALOG,AfxGetApp()-> ...

  7. 10. Halloween 万圣节

    10. Halloween 万圣节 (1) On October the 31st,across Britain and the USA,thousands of children are dress ...

  8. Codeforces Round #540 (Div. 3)--1118C - Palindromic Matrix

    https://codeforces.com/contest/1118/problem/C 在查找元素的时候,必须按4,2,1的顺序进行.因为,如果先找1,可能就把原来的4拆散了,然后再找4,就找不到 ...

  9. Centos yum 修改为阿里源以及常用的命令

    1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载新的CentOS-Base ...

  10. Django Template 进阶

    回顾: Variables {{ var }} {{ dict.key }} {{ var.attr }} {{ var.method }} {{ varindex }} Filter {{ list ...