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 ...
随机推荐
- maven的知识图谱
maven 1.maven的好处 java项目管理工具 依赖管理 对jar包统一管理 项目名称 公司/组织 版本信息 本地仓库 由于索引的存在,找jar包很快 项目构建 依赖管理 传统项目 很大 包含 ...
- linux中,在在shadowsoks下怎么更新软件
在shadowsoks下怎么更新软件 先描述一下我的情况.我们学校不给大一开通校园网,我自己租用了***的vps服务器,搭建shadowsocks,(使用ipv6地址才能连接),开始了自己悲催的上网生 ...
- apache去掉目录浏览
apache去掉目录浏览 apache默认开启目录浏览的,这样大大降低了我们网站的安全,下面是关闭浏览目录: 要禁止 Apache 显示目录结构列表,只需将 Option 中的 Indexes 去掉即 ...
- Mycat查询时出现:Error Code: 1064. can't find any valid datanode
说明:这个错误是查询时条件字段超过了范围导致的. 解释: 为什么会出现范围问题? 一般在MySQL查询时不会因为字段的值超过了范围而导致的,但是在Mycat中却不是这样认为的,Mycat是由于采用的分 ...
- Quartz配置CronTrigger
CronTrigger 构造器 String name, //触发器名称 String group, //触发器的组名 String jobName, //job名称 String jobGroup, ...
- react数组key的唯一性
1.不要使用数组的index索引作为key 2.在相邻的元素间,一定确保key的唯一性,如果出现了相同的 key,会抛出一个 Warning,告诉相邻组件间有重复的 key 值.并且只会渲染第一个重复 ...
- ckeditor的详细配置(转)
CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...
- ElasticSearch 监控单个节点详解
1.介绍 集群健康 就像是光谱的一端——对集群的所有信息进行高度概述. 而 节点统计值 API 则是在另一端.它提供一个让人眼花缭乱的统计数据的数组,包含集群的每一个节点统计值. 节点统计值 提供的统 ...
- Spring配置xml版
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Assets 读取assets中的文件
res/raw和assets的相同点: 1.两者目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制. res/raw和assets的不同点:1.res/raw中的文件会被映射到R.j ...