.NetCore中EFCore的使用整理
EntirtyFramework框架是一个轻量级的可扩展版本的流行实体框架数据访问技术.
其中的.NetCore版本对应EntityFrameworkCore
Git源代码地址:https://github.com/aspnet/EntityFramework/
官方使用文档说明:https://docs.microsoft.com/zh-cn/ef/core/index
一、安装Nuget包
Install-package Microsoft.EntityFrameworkCore
Install-package Microsoft.EntityFrameworkCore.SqlServer
Micorsoft.EntityFrameworkCore:EF框架的核心包
Micorsoft.EntityFrameworkCore.SqlServer:针对SqlServer数据库的扩展,使用SqlServer数据库必须。类似的还有MySql,SqlLite等
Micorsoft.EntityFrameworkCore.Tools
&Micorosft.EntityFrameworkCore.Design:用户根据现有的数据库生成模型代码等 ,更多参考 :https://docs.microsoft.com/zh-cn/ef/efcore-and-ef6/porting/port-edmx二、使用实例
1.安装Nuget包之后,手动创建上下文,并 注入sql链接字符串
using Microsoft.EntityFrameworkCore;
namespace Core2
{
public class TestContext : DbContext
{
//public TestContext(DbContextOptions<TestContext> options) : base(options)
//{ //}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//注入Sql链接字符串
optionsBuilder.UseSqlServer(@"Server=.;Database=Test1;Trusted_Connection=True;");
}
public DbSet<Numeber1> Numeber1s { get; set; }
}
}
2.手写实体类,只要数据库中 存在对应 的表就可以了
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Core2
{
[Table("Numeber1")]
public class Numeber1
{
[Key]
public int ID { get; set; }
public decimal Num1 { get; set; }
}
}
3.测试代码:
static void TestOne()
{
TestContext _context = new TestContext();
int count = _context.Numeber1s.Count();
Console.WriteLine(count);
}
static void TestTwo()
{
DateTime start = DateTime.Now;
TestContext _context = new TestContext();
for (int i = ; i < ; i++)
{
_context.Numeber1s.Add(new Numeber1()
{
Num1 = i
});
_context.SaveChanges();
}
Console.WriteLine(_context.Numeber1s.Count());
Console.WriteLine("总时间,秒数:" + (DateTime.Now - start).TotalSeconds);
}
在调试的状态下1万条插入数据执行时间:

三、根据数据库生成模型
1.安装EntityFrameworkCore.Design,EntityFrameworkCore.Tools,EntityFrameworkCore.SqlServer.Design
注:在EFCore2.0中只需要安装
EntityFrameworkCore.Tools
如果不需要自动根据数据库生成代码,这个几个类库可以不安装 。
Install-package Microsoft.EntityFrameworkCore.Design
Install-package Microsoft.EntityFrameworkCore.Tools
Install-package Microsoft.EntityFrameworkCore.SqlServer.Design
2.选择对应的项目,执行生成命名
Scaffold-DbContext "Server=.;database=test1;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models2

3.生成结果如下 :
四、Asp.Net Core中注册EF的上下文处理,在Startup文件中
1.注册服务
//配置EF的服务注册
services.AddEntityFramework()
.AddDbContext<NotifyBirdContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("SqlServer"), //读取配置文件中的链接字符串
b => b.UseRowNumberForPaging()); //配置分页 使用旧方式
});
2.修改上下文,重点指定DbContextOptions有外部配置
public NotifyBirdContext(DbContextOptions<NotifyBirdContext> opt) : base(opt)
{ }
3.在控制器中使用数据库上下文服务
NotifyBirdContext _Context = null;
public ProjectController(NotifyBirdContext context)
{
_Context = context;
}
/// <summary>
/// 获取可用项目数量
/// </summary>
/// <returns></returns>
[HttpGet("getcount")]
public int GetCount()
{
try
{
return _Context.Project.Count();
}
catch (Exception ex)
{ throw ex;
}
}
五 、.Net Core中 EF Core上下文配置 2,使用全局变量方式定义链接字符串
1.使用空参数构造器的上下文
/// <summary>
/// 全局定义数据连接字符串
/// </summary>
public static string ConStr { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;database=NotifyBird;Trusted_Connection=True;"); //配置数据链接
optionsBuilder.UseSqlServer(ConStr,b=>b.UseRowNumberForPaging());
}
2.程序 启动注册链接
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(); NotifyBirdContext.ConStr = Configuration.GetConnectionString("SqlServer");
}
3.任意位置实例化,上下文使用
/// <summary>
/// 获取可用项目数量
/// </summary>
/// <returns></returns>
[HttpGet("getcount")]
public int GetCount()
{
try
{
NotifyBirdContext _Context = new NotifyBirdContext();
return _Context.Project.Count();
}
catch (Exception ex)
{ throw ex;
}
}
更多 :
VS Code搭建.NetCore开发环境(二)
VS Code搭建.NetCore开发环境(一)
Chocolatey 简介(软件自动化管理工具)
.NetCore中EFCore的使用整理的更多相关文章
- .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中EFCore for MySql整理
一.MySql官方提供了Ef Core对MySql的支持,但现在还处于预览版 Install-Package MySql.Data.EntityFrameworkCore -Pre Install-P ...
- NetCore 中 EFcore的DbFirst和CodeFirst混合 使用注意
NetCore 最近很火热.笔者想把自己以前的旧项目迁移到NetCore平台. 先用EFcore的DBFirst根据数据库创建实体类,然后加入数据库版本控制功能也就是EFcore的CodeFirst部 ...
- Asp.Net Core中Json序列化处理整理
一.Asp.Net Core中的Json序列化处理使用的是Newtonsoft.Json,更多参考:C# Newtonsoft.Json JsonSerializerSettings配置序列化操作,C ...
- netcore使用EFcore(第一个实例)
说明:搭建netcore 使用efcore入门教程,跟着这个教程,傻瓜都可以成功!O(∩_∩)O哈哈~,咱们开始吧: 首先介绍下环境: vs2017, netcore2.2, EntityFramew ...
- .NetCore中的日志(2)集成第三方日志工具
.NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...
随机推荐
- ZOJ 3229 Shoot the Bullet(有源汇上下界最大流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3442 题目大意: 一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝给 ...
- hdu5289 ST表+二分
用裸的St表+暴力枚举查询时稳TLE的,可以枚举每个区间的起点+二分满足条件的区间右端,这样复杂度是O(nlogn) #include<iostream> #include<cstr ...
- python 全栈开发,Day68(Django的路由控制)
昨日内容回顾 1 MVC和MTV MTV 路由控制层(分发哪一个路径由哪一个视图函数处理) V : views (逻辑处理) T : templates (存放html文件) M : model (与 ...
- Number对象的常用属性和方法
方法 描述 isNan() 检查值是否为数字 toFied() 将特定数字四舍五入至小数位数(返回一个字符串) toPrecision() 按数字的位数四舍五入(返回一个字符串) toExponent ...
- 《剑指offer》-左旋转字符串
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S="abc ...
- -webkit-line-clamp超出省略
以前只用过超出一行显示省略号 有时候会碰到只显示两到三行,超出省略 -webkit-line-clamp属性就能解决这个问题 text-overflow: -o-ellipsis-lastline; ...
- python全栈开发day19-面向对象初识
1.昨日内容回顾 模块: 1.什么是模块,什么是包 py文件就是模块,包是包含一系列py文件(__init__.py)的文件夹. 2.模块的导入相当于相当于执行了导入的模块,首次 ...
- 解决celipse中mybatis使用的时候xml没有提示的问题
文件下载好以后点击Add会出现如下界面 上面是添加mapper的插件,再找到config的配置文件.和上面相似的操作安装以后就可以了
- 095实战 ETL的数据来源,处理,保存
1.ETL 数据来源:HDFS 处理方式:Mapreduce 数据保存:HBase 2.为什么保存在Hbase中 数据字段格式不唯一/不相同/不固定,采用hbase的动态列的功能非常适合 因为我们的分 ...
- hdu 2063 过山车【匈牙利算法】(经典)
<题目链接> RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partne ...

