目前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. Properties文件工具读取类

    import java.io.IOException;import java.io.InputStream;import java.util.Properties; public class Comm ...

  2. bzoj 5163: 第k大斜率

    5163: 第k大斜率 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 15  Solved: 4[Submit][Status][Discuss] D ...

  3. POJ 1795 DNA Laboratory(状压DP)

    [题目链接] http://poj.org/problem?id=1795 [题目大意] 给出n个字符串,求一个最小长度的串,该串包含给出的所有字符串. 要求长度最小且字典序最小. [题解] dp[i ...

  4. [xsy2213]tower

    题意:给一个地图,地图上的每个位置是空地或一个炮或一些敌人,给定每个炮的方向(上/下/左/右),每个炮只能打一个位置且炮弹轨迹不能相交,问最多打到多少敌人 原题貌似是TC SRM 627的题,题解在这 ...

  5. Apache压力(并发)测试工具ab的使用教程收集

    说明:用ab的好处,在处理多并发的情况下不用自己写线程模拟.其实这个世界除了LoadRunner之外还是有很多方案可以选择的. 官网: http://httpd.apache.org/(Apache服 ...

  6. VS2010免费插件

    用惯了VC助手后,突然用裸的VS真不习惯... 1. 在Visual Studio 2010中,我们可以通过在任何代码文件中使用快捷键“Ctrl + ,”(Ctrl键加上逗号键)调出“Quick Se ...

  7. JAVA常见算法题(一)

    package com.xiaowu.demo; // 有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第四个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少? /** * ...

  8. [置顶] kubernetes--优雅删除资源对象

    当用户请求删除含有pod的资源对象时(如RC.deployment等),K8S为了让应用程序优雅关闭(即让应用程序完成正在处理的请求后,再关闭软件),K8S提供两种信息通知: 1).默认:K8S通知n ...

  9. nginx,wsgi,django的关系

    http://blog.csdn.net/lihao21/article/details/52304119 wsgi用于连续 nginx和django,客户端发来的请求,先经过wsgi,然后再传给dj ...

  10. IT开发者对Mac钟爱

    由于Mac的操作系统OSX相比Windows win7/8/10来说,比較适合开发者使用.个人的体会例如以下: 首先.OSX的多窗体多应用程序切换功能非常强大,对开发者来说非常实用.开发者一般都须要开 ...