整理一下目前在用的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的基本使用的更多相关文章

  1. Re0:在.NetCore 中Dapper的基本用法

    整理一下目前在用的Dapper 与FrameWork不同,NetCore数据库配置需要从appsettings.json中获取 刚接触的时候被这块坑了,自己手动建了个app.config.然后你懂的( ...

  2. .NetCore中EFCore的使用整理

    EntirtyFramework框架是一个轻量级的可扩展版本的流行实体框架数据访问技术. 其中的.NetCore版本对应EntityFrameworkCore Git源代码地址:https://git ...

  3. .NetCore中EFCore的使用整理(二)-关联表查询

    EF常用处理关联加载的方式有3中:延迟加载(Lazy Loading).贪婪加载 (Eager Loading)以及显示加载. 一.EF Core  1.1 1.当前的版本,还不支持延迟加载(Lazy ...

  4. .NetCore中EFCore for MySql整理(三)之Pomelo.EntityFrameworkCore.MySql

    一.Pomelo.EntityFrameworkCore.MySql简介 Git源代码地址:https://github.com/PomeloFoundation/Pomelo.EntityFrame ...

  5. .NetCore中EFCore for MySql整理(二)

    一.简介 EF Core for MySql的官方版本MySql.Data.EntityFrameworkCore 目前正是版已经可用当前版本v6.10,对于以前的预览版参考:http://www.c ...

  6. .NetCore中EFCore的使用整理(三)-关联表操作

    一.查询关联表数据 StudyAboard_TestContext _context = new StudyAboard_TestContext(); CrmRole role = _context. ...

  7. .NetCore中的日志(2)集成第三方日志工具

    .NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...

  8. .NetCore中的日志(1)日志组件解析

    .NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...

  9. .NetCore之EF跳过的坑

    我在网上看到很多.netCore的信息,就动手自己写一个例子测试哈,但是想不到其中这么多坑: 1.首先.netCore和EF的安装就不用多说了,网上有很多的讲解可以跟着一步一步的下载和安装,但是需要注 ...

随机推荐

  1. python_模块2

    1.sys模块 import sys # 获取一个值的应用计数 a = [11,22,33] b = a print(sys.getrefcount(a)) # python默认支持的递归数量 v1 ...

  2. 四、vue基础--自定义组件

    1.语法:Vue.component("组件名字",{data,template}),代码如下: a. data: 必须是一个函数,有一个返回值.和vue里面的使用方法一样 b. ...

  3. C语言I作业12一学期总结

    一.我学到的内容 二.我的收获 作业 收获 C语言I博客作业01 学会了编程"Hello word" C语言I博客作业02 安装编译器,将代码建立在自己的文件里面 C语言I博客作业 ...

  4. C# LINQ标准查询操作符

    首先添加数据集合 [Serializable] public class Racer : IComparable<Racer>, IFormattable { public Racer() ...

  5. C语言学习系列(一)开门首篇

    一.特辑 此次是我自己的学习之路,和大家一起分享(我现在是做Java),途中遇到什么问题大家也可以提出来一起讨论一起进步: 主要参考教程是菜鸟教程上面的C语言教程,以及大学课本C语言教程-第四版(谭浩 ...

  6. Node.js 调试小技巧

    小技巧--使用 supervisor如果你有 PHP 开发经验,会习惯在修改 PHP 脚本后直接刷新浏览器以观察结果,而你在开发 Node.js 实现的 HTTP 应用时会发现,无论你修改了代码的哪一 ...

  7. 组件化网页开发 / 步骤二 · 2-11 jquery的ajax方法 以及下一章跨域没懂

    1,根据2-11课程老师的提示,自己封装 $.ajax 2,第三章,跨域,没看懂

  8. BZOJ 1420 Discrete Root

    思路:数学大汇总 提交:\(3\)次 错因:有一个\(j\)写成\(i\) 题解: 求:\(x^k \equiv a \mod p\) 我们先转化一下:求出\(p\)的原根\(g\) 然后我们用\(B ...

  9. 为什么用到springboot?

  10. [Luogu] 矩形覆盖

    https://www.luogu.org/problemnew/show/P1034 数据太水 爆搜过掉 #include <iostream> #include <cstdio& ...