整理一下目前在用的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. Java锁--公平锁

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3496147.html 基本概念 本章,我们会讲解“线程获取公平锁”的原理:在讲解之前,需要了解几个基本概 ...

  2. idea jsp文件中body标签内引入编辑器后提示statement expected

    解决方案: 1.用标签将script包一层解决问题 2.或者在</script>后添加自闭和标签(推荐) <input/>.<img/> //等等自闭和标签

  3. tableviewer自动调整列宽

    public void resizeTableColumn(TableColumn[] treeColumns) { for (TableColumn tc : treeColumns) tc.pac ...

  4. [Google Guava] 12-数学运算

    原文链接 译文链接 译者:沈义扬 范例 1 int logFloor = LongMath.log2(n, FLOOR); 2 int mustNotOverflow = IntMath.checke ...

  5. FWT 等总结 题解

    目录 与卷积: 代码: 或卷积: 代码: 异或卷积: 代码: FST:子集卷积 代码: 例题: CF914G 代码: uoj310[UNR #2]黎明前的巧克力 代码: CF662C Binary T ...

  6. Activiti工作流学习(一)——Activiti服务类

    Activity有9个service1.DynamicBpmnService动态Bpmn服务Service providing access to the repository of process ...

  7. leetcode解题报告(3):Search in Rotated Sorted Array

    描述 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

  8. 【概率论】4-5:均值和中值(The Mean and the Median)

    title: [概率论]4-5:均值和中值(The Mean and the Median) categories: - Mathematic - Probability keywords: - Me ...

  9. python 将IP地址转换成打包后的32位格式

    python 2.7 #!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - # This program r ...

  10. scrapy框架之log日志

    scrapy中的debug信息 在scrapy中设置log 1.在settings中设置log级别,在settings.py中添加一行: Scrapy提供5层logging级别: CRITICAL - ...