目前EF Core调用存储过程,限制很多,比如返回结果必须是定义好的DbSet<>等等。这里用一种曲线救国的方式,自定义两个方法,用原始ado.net解决问题。以MySql数据库为例,代码如下:

 namespace WebApi.Core.Data
{
using System.Data;
using System.Linq; using Microsoft.EntityFrameworkCore; using MySql.Data.MySqlClient; public class TestContext : DbContext
{
public TestContext(DbContextOptions<TestContext> options)
: base(options)
{
} public int ExcuteProcedure(string parcedureName, params MySqlParameter[] ps)
{
using (var cmd = this.Database.GetDbConnection().CreateCommand())
{
cmd.CommandText = parcedureName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(ps);
this.Database.OpenConnection();
var r = cmd.ExecuteNonQuery();
this.Database.CloseConnection();
return r;
}
} public object ExcuteFunction(string functionName, params MySqlParameter[] ps)
{
using (var cmd = this.Database.GetDbConnection().CreateCommand())
{
// 参数字符串,传参时注意顺序
var pString =
$"'{ps.Aggregate(string.Empty, (c, n) => $"{c}','{n.Value}").TrimStart(new[] { ',', '\'' })}'"; // 可用this.Database.IsXXX(),判读数据库类型,拼command字符串,这里不写了
cmd.CommandText = $"select {functionName}({pString});";
this.Database.OpenConnection();
var r = cmd.ExecuteScalar();
this.Database.CloseConnection();
return r;
}
} protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
}
}
}

entity framework core 调用存储过程和方法的更多相关文章

  1. .Net Entity Framework Core 用 HasColumnType 配置浮点数精度

    一.前言 前段时间用.Net Entity Framework core搭建框架,需要配置浮点数的精度,发现.Net Entity Framework core 并没有HasPrecision方法.在 ...

  2. Entity Framework Core 执行SQL语句和存储过程

    无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...

  3. Entity Framework Core生成的存储过程在MySQL中需要进行处理及PMC中的常用命令

    在使用Entity Framework Core生成MySQL数据库脚本,对于生成的存储过程,在执行的过程中出现错误,需要在存储过程前面添加 delimiter // 附:可以使用Visual Stu ...

  4. Entity Framework Core 1.1 升级通告

    原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...

  5. Entity Framework Core 1.1 Preview 1 简介

    实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...

  6. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据

    Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...

  7. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据

    Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...

  8. 使用 Entity Framework Core 时,通过代码自动 Migration

    一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...

  9. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio »迁移

    Migrations¶ 4 of 4 people found this helpful The Contoso University sample web application demonstra ...

随机推荐

  1. Python与数据库[1] -> 数据库接口/DB-API[2] -> SQL Server 适配器

    SQL_Server适配器 / SQL_Server Adapter 1 环境配置 / Environment Configuration 安装SQL_Server的Python适配器包 pip in ...

  2. MySQL-python install

    Redhat 7.6 yum install python-devel mysql-devel gcc -y pip install MySQL-python==1.2.5

  3. SpringCloud集群(三)

    一.构造步骤 1.进行其他的服务中心的域名映射 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com 2 ...

  4. linux中,在在shadowsoks下怎么更新软件

    在shadowsoks下怎么更新软件 先描述一下我的情况.我们学校不给大一开通校园网,我自己租用了***的vps服务器,搭建shadowsocks,(使用ipv6地址才能连接),开始了自己悲催的上网生 ...

  5. POJ 3713 Transferring Sylla (三连通图)

    [题目链接] http://poj.org/problem?id=3713 [题目大意] 给出一个图判断是不是三连通图,三连通图的意思是对于图中任意两点, 至少有三条路是可以相互连通的. [题解] 我 ...

  6. 2.3多线程(java学习笔记)synchronized关键字

    一.为什么要用synchronized关键字 首先多线程中多个线程运行面临共享数据同步的问题. 多线程正常使用共享数据时需要经过以下步骤: 1.线程A从共享数据区中复制出数据副本,然后处理. 2.线程 ...

  7. Linux Programmer's Manual --- reboot

    REBOOT(2) Linux Programmer's Manual REBOOT(2) NAME reboot - reboot or enable/disable Ctrl-Alt-Del SY ...

  8. python数据类型学习心得

    python中的数据类型 数字:整型,长整形,布尔型,浮点型,复数 整型:普通的整数,在32位的操作系统中范围在-2的-32次方到2的32次方-1,64位的操作系统则为-2的64次方到2的64次方-1 ...

  9. 数据库字段名称转java字段名称

    /** * * @Title: changeToJavaFiled * @Description: TODO(将数据库中带下划线的字段转换为Java常用的驼峰字段) * @param @param f ...

  10. 10. 修改端口号【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51637017 spring boot 默认端口是8080,如果想要进行更改的话,只需要修改 ...