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的使用整理的更多相关文章

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

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

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

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

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

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

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

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

  5. .NetCore中EFCore for MySql整理

    一.MySql官方提供了Ef Core对MySql的支持,但现在还处于预览版 Install-Package MySql.Data.EntityFrameworkCore -Pre Install-P ...

  6. NetCore 中 EFcore的DbFirst和CodeFirst混合 使用注意

    NetCore 最近很火热.笔者想把自己以前的旧项目迁移到NetCore平台. 先用EFcore的DBFirst根据数据库创建实体类,然后加入数据库版本控制功能也就是EFcore的CodeFirst部 ...

  7. Asp.Net Core中Json序列化处理整理

    一.Asp.Net Core中的Json序列化处理使用的是Newtonsoft.Json,更多参考:C# Newtonsoft.Json JsonSerializerSettings配置序列化操作,C ...

  8. netcore使用EFcore(第一个实例)

    说明:搭建netcore 使用efcore入门教程,跟着这个教程,傻瓜都可以成功!O(∩_∩)O哈哈~,咱们开始吧: 首先介绍下环境: vs2017, netcore2.2, EntityFramew ...

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

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

随机推荐

  1. 浏览器的userAgent归纳

    IE IE6 User-Agent:Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2) IE7 User-Agent:Mozilla/4.0 (compat ...

  2. Storm的部署

    配置方案如下 node1 Nimbus zookeeper node2 Supervisor zookeeper node3 Supervisor zookeeper node4 Supervisor ...

  3. python + selenium 模块封装及参数化

    模块封装 示例代码: baidu.py from time import sleep from selenium import webdriver driver = webdriver.Chrome( ...

  4. Linux学习笔记:使用ftp命令上传和下载文件

    Linux中如何使用ftp命令,包括如何连接ftp服务器,上传or下载文件以及创建文件夹.虽然现在有很多ftp桌面应用(例如:FlashFXP),但是在服务器.SSH.远程会话中掌握命令行ftp的使用 ...

  5. LeetCode | Reverse Words in a String(C#)

    题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is b ...

  6. 通过T4模板实现代码自动生成

    1:准备.tt模板 using BBFJ.OA.IBLL; using BBFJ.OA.IDAL; using BBFJ.OA.Model; using System; using System.Co ...

  7. JS高级 - 面向对象4(json方式面向对象)

    把方法包在一个Json里 var p1 = { name: "唐三", sex: "男", dreamdu: { URL: "www.dreamdu. ...

  8. PowerDesigner使用积累

    PowerDesigner想必没人不知道吧?著名的CASE工具,目前最新版本为15.2,用于软件建模,可以从需求直到物理模型,支持UML2.0语法,可用于UML图绘制.最大特色是能够使设计到实现无缝衔 ...

  9. python界面Tkinter编程(tkMessageBox对话框使用)

    python界面Tkinter编程(tkMessageBox对话框使用)     转载 https://blog.csdn.net/m_buddy/article/details/80105154 1 ...

  10. Mac配置Eclipse CDT的Debug出现的问题(转)

      问题1:出现 Could not determine GDB version using command: gdb --version 原因: mac上没有安装gdb或者gdb位置配置有问题 解决 ...