entity framework core 调用存储过程和方法
目前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 调用存储过程和方法的更多相关文章
- .Net Entity Framework Core 用 HasColumnType 配置浮点数精度
一.前言 前段时间用.Net Entity Framework core搭建框架,需要配置浮点数的精度,发现.Net Entity Framework core 并没有HasPrecision方法.在 ...
- Entity Framework Core 执行SQL语句和存储过程
无论ORM有多么强大,总会出现一些特殊的情况,它无法满足我们的要求.在这篇文章中,我们介绍几种执行SQL的方法. 表结构 在具体内容开始之前,我们先简单说明一下要使用的表结构. public clas ...
- Entity Framework Core生成的存储过程在MySQL中需要进行处理及PMC中的常用命令
在使用Entity Framework Core生成MySQL数据库脚本,对于生成的存储过程,在执行的过程中出现错误,需要在存储过程前面添加 delimiter // 附:可以使用Visual Stu ...
- Entity Framework Core 1.1 升级通告
原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...
- Entity Framework Core 1.1 Preview 1 简介
实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...
- 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 ...
- 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 ...
- 使用 Entity Framework Core 时,通过代码自动 Migration
一 介绍 在使用 Entity Framework Core (下面就叫 EF Core 吧)进行开发时,如果模型有变动,我们要在用 EF Core 提供的命令行工具进行手工迁移,然后再运行程序.但是 ...
- 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 ...
随机推荐
- Python的网络编程[1] -> FTP 协议[1] -> 使用 pyftplib 建立 FTP 服务器
使用 pyftplib 建立 FTP 服务器 pyftplib 主要用于建立 FTP Server,与 ftplib 建立的 Client 进行通信. 快速导航 1. 模块信息 2. 建立 FTP 服 ...
- 在sae中运行web.py应用
sae 是新浪推出的PaaS业务,可以提供免运维的容器服务,官方网站( https://www.sinacloud.com/ ) 假设您已经在本地开发好了web.py 应用,您可以通过github客户 ...
- [BZOJ1194][HNOI2006][强连通分量Tarjan+dfs]潘多拉的盒子
[BZOJ1194][HNOI2006]潘多拉的盒子 Input 第一行是一个正整数S,表示宝盒上咒语机的个数,(1≤S≤50).文件以下分为S块,每一块描述一个咒语机,按照咒语机0,咒语机1„„咒语 ...
- sql-----STR 函数
原文:sql-----STR 函数 sql-----STR 函数 STR 函数由数字数据转换来的字符数据. 语法 STR ( float_expression [ , length [ , decim ...
- ASIHTTPRequest学习(一)
Creating a synchronous request 可以创建同步和异步两种方式的请求,一般情况下应当使用异步请求.使用同步请求主应用线程会锁住直至解锁为止. 创建异步请求,会在后台执行 - ...
- C++STL中的向量vector
#include<iostream>#include<vector>#include<algorithm>using namespace std;typedef v ...
- php设计模式之建造者模式
建造者模式 建造者设计模式的目的是消除其他对象的复杂创建过程.使用建造者设计模式不仅是最佳的做法,而且在摸个对象的构造和配置方法改变时候,可以尽可能的减少重复更改代码. <?php /** *p ...
- DotnetBrowser高级教程-(5)使用内置的MVC UI框架-EasyMvc
如果DotnetBrowser只是实现了内置chrome浏览器和web/web socket server,似乎还不是很完美.因此,最新的DotnetBrowser已经内置对easy mvc控件的支持 ...
- ylbtech_dbs_article_五大主流数据库模型
ylbtech_dbs_article 摘要:什么是数据模型? 访问数据库中的数据取决于数据库实现的数据模型.数据模型会影响客户端通过API对数据的操作.不同的数据模型可能会提供或多或少的功能.一般而 ...
- 18 Tar Command Examples in Linux
FROM: http://www.tecmint.com/18-tar-command-examples-in-linux/ 18 Tar Command Examples in Linux By R ...