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的安装就不用多说了,网上有很多的讲解可以跟着一步一步的下载和安装,但是需要注 ...
随机推荐
- css引用优先级
/***************************************css注意事项*******************************************/ 浏览器优先级:设 ...
- 自己编写jQuery插件之表单验证
自己编写jQuery插件之表单验证 吐个嘈先:最近状态不咋滴,真是什么都不想干,不想上班,做什么都没动力,觉得没意思.不想这样,不想这样,快让这种情绪消失吧,忽忽.... 表单验证在项目中用的还是比较 ...
- mybatis+redis实现二级缓存
在网上看了很多资料,发现例子都是千篇一律的相互复制.而且,使用的都是jedis的客户端..我这里使用的是redistemplate类实现. 缓存的原理..实现cache类接口,当哪个类需要缓存的时候, ...
- Python 8--异常
- HTML元素常用属性整理
a标签 <!-- 去除下划线 --> a{ text-decoration:none; //去掉默认下滑线 color:#333; //设置默认颜色 } a:hover{ text-dec ...
- 字节(byte)、二进制、字节流、字符流相关概念分析
https://blog.csdn.net/changwilling/article/details/52065955 1.字节: 字(Byte)节是长度单位.位(bit)也是长度单位. 因为计算机通 ...
- VS2012 Update 2: 0x80040154 corrupt install when starting the debugger
使用VS2012開發console program ,发现生成32位的exe文件在別的机上不能正确运行,有文章說update1可以解決這個問題,如下 Setup.exe is not a valid ...
- 在Vue中使用TypeScript
TypeScript基础学习 初始化TS项目 Vue装饰器 vue-typescript-admin框架 1.TypeScript基础学习 2.初始化TS项目 3.Vue装饰器 Vue装饰器常用的有下 ...
- OpenPyXl的使用
OpenPyXl的使用 开始在内存中使用 创建一个workbook 在刚开始使用openpyxl的时候,不需要直接在文件系统中创建一个文件,仅仅需要导入Workbook类并开始使用它: >> ...
- Python学习日记(四)——Python基本数据类型梳理(int、str、list、tuple、dict)
数字(int) 1.创建方式 n1 = 123 n2 = int(123) 2.内存分配 #共同用一个内存地址的情况 n1 = 123 n2 = n1 #用两个内存地址的情况 n1 = 123 n2 ...