Re0:在 .NetCore中 EF的基本使用
整理一下目前在用的EFCore
记得好像是因为懒得写sql,于是开始试着用EF
先根据数据库生成一个好东西,嗯
Scaffold-DbContext "Data Source=localhost;Initial Catalog=dbname;Persist Security Info=True;User ID=admin;Password=admin;Pooling=true;Max Pool Size=512; Min Pool Size=5;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
没有sql语句美滋滋
#region 添加
/// <summary>
/// Add
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
[HttpPost("Add")]
public IActionResult Add([FromBody] T obj)
{
try
{
_context.T.Add(obj);
var result = _context.SaveChanges().ToString(); responseData.Status = ;
responseData.Data = result;
responseData.Message = "Success";
}
catch (Exception ex)
{
responseData.Status = ;
responseData.Message = ex.Message;
return new JsonResult(responseData);
} return new JsonResult(responseData);
}
#endregion
一时不用一时爽
#region 删除
//删除
[HttpDelete("Delete")]
public IActionResult Delete([FromBody] T obj)
{
try
{
var Test = new T { Id = obj.Id };
_context.T.Remove(Test);
var result = _context.SaveChanges().ToString(); responseData.Status = ;
responseData.Data = result;
responseData.Message = "Success";
}
catch (Exception ex)
{
responseData.Status = ;
responseData.Message = ex.Message;
return new JsonResult(responseData);
} return new JsonResult(responseData);
}
#endregion
一直不用一直爽
#region 修改
//修改
[HttpPut("Modify")]
public IActionResult Modify([FromBody] T obj)
{
try
{
int result = -;
T t = _context.T.SingleOrDefault(u => u.Id == obj.Id); if (t != null)
{
t = obj; result = _context.SaveChanges();
} responseData.Status = ;
responseData.Data = result;
responseData.Message = "Success";
}
catch (Exception ex)
{
responseData.Status = ;
responseData.Message = ex.Message;
return new JsonResult(responseData);
} return new JsonResult(responseData);
}
#endregion
那么问题来了,想sql了怎么办?
能用Sql,但是不推荐;原因有机会了再补充
#region EFSql
//Select
[HttpGet("Query")]
public IActionResult Query()
{
try
{
string sql = "select * from bula bula bula"; var result = _context.Set<Model>().FromSql(sql).ToList(); if (result.Count() == )
{
responseData.Status = ;
responseData.Message = "数据为空"; return new JsonResult(responseData);
} responseData.Status = ;
responseData.Data = result;
responseData.Message = "Success";
}
catch (Exception ex)
{
responseData.Status = ;
responseData.Message = ex.Message;
return new JsonResult(responseData);
}
return new JsonResult(responseData);
}
#endregion
Re0:在 .NetCore中 EF的基本使用的更多相关文章
- Re0:在.NetCore 中Dapper的基本用法
整理一下目前在用的Dapper 与FrameWork不同,NetCore数据库配置需要从appsettings.json中获取 刚接触的时候被这块坑了,自己手动建了个app.config.然后你懂的( ...
- .NetCore中EFCore的使用整理
EntirtyFramework框架是一个轻量级的可扩展版本的流行实体框架数据访问技术. 其中的.NetCore版本对应EntityFrameworkCore Git源代码地址:https://git ...
- .NetCore中EFCore的使用整理(二)-关联表查询
EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core 1.1 1.当前的版本,还不支持延迟加载(Lazy ...
- .NetCore中EFCore for MySql整理(三)之Pomelo.EntityFrameworkCore.MySql
一.Pomelo.EntityFrameworkCore.MySql简介 Git源代码地址:https://github.com/PomeloFoundation/Pomelo.EntityFrame ...
- .NetCore中EFCore for MySql整理(二)
一.简介 EF Core for MySql的官方版本MySql.Data.EntityFrameworkCore 目前正是版已经可用当前版本v6.10,对于以前的预览版参考:http://www.c ...
- .NetCore中EFCore的使用整理(三)-关联表操作
一.查询关联表数据 StudyAboard_TestContext _context = new StudyAboard_TestContext(); CrmRole role = _context. ...
- .NetCore中的日志(2)集成第三方日志工具
.NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...
- .NetCore中的日志(1)日志组件解析
.NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...
- .NetCore之EF跳过的坑
我在网上看到很多.netCore的信息,就动手自己写一个例子测试哈,但是想不到其中这么多坑: 1.首先.netCore和EF的安装就不用多说了,网上有很多的讲解可以跟着一步一步的下载和安装,但是需要注 ...
随机推荐
- linux实操_rpm包和yum包
rpm包的简单查询指令: 查询已安装的rpm列表 rpm -qa | grep xxx 查询火狐浏览器 查询安装的rpm包软件的信息 查询rpm软件包的文件安装在哪里 查询文件属于哪个软件包 卸载rp ...
- ASP.Net模拟用户 System.Security.Principal
一.概述 在实际的项目开发中,我们可能会需要调用一些非托管程序,而有些非托管程序需要有更高的身份权限才能正确执行.本文介绍了如何让IIS承载的ASP.NET网站以特定的账户执行,比如Administr ...
- 注册emf package并读取EMF文件
/** * 读EMF文件 * * @param uri * @return */ public static Resource readEMFFile(URI uri) { ResourceSet r ...
- PHP mysqli_error() 函数
返回最近调用函数的最后一个错误描述: <?php // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect("localh ...
- PHP mysqli_character_set_name() 函数
返回数据库连接的默认字符集: <?php $con=mysqli_connect("localhost","my_user","my_passw ...
- Shell 06 awk
一.基本操作方法 ######################################### grep 按行进行查找 vim 编辑文档,交互式 ####################### ...
- [Luogu] 1600
https://www.luogu.org/problemnew/show/P1600 nlogn竟然T了 #include <iostream> #include <cstdio& ...
- LOJ166 拉格朗日插值 2【卷积,NTT】
题目链接:LOJ 题目描述:输入多项式的次数$n$,一个整数$m$和$f(0),f(1),f(2),\ldots,f(n)$,输出$f(m),f(m+1),f(m+2),\ldots,f(m+n)$ ...
- 第十七节:Runnable创建线程,Thread创建线程,唤醒线程和计数器多线程,线程同步与等待
Runnable创建线程 public class RunnableDemo implements Runnable{ @Override public void run(){ int i = 1; ...
- vue中插槽(slot)的使用
刚学vue的时候,曾经学习过slot插槽的使用,但是后面接触的不多,因为之前我还没使用element-ui... 但是使用了element-ui之后,里面的许多组件,有时候会使用插槽,为了巩固一下插槽 ...