一、安装EFCode包

  EFCore需要根据不同的数据库选择不同的数据库提供程序database provider,各数据库的包地址:https://docs.microsoft.com/zh-cn/ef/core/providers/

使用sqlserver数据库使用的是Microsoft.EntityFrameworkCore.SqlServer包,支持SQL Server 2008 及以上版本

Microsoft.EntityFrameworkCore.SqlServer包依赖Microsoft.EntityFrameworkCore 和Microsoft.EntityFrameworkCore.Relational两个包

  新建Core2 Web Application (Model-View-Controller)项目,Microsoft.EntityFrameworkCore.SqlServer  已经默认安装到了 Microsoft.AspNetCore.All元包里面,不需要再次安装

二、建立实体并添加EF上下文

  给每个实体创建一个DbSet属性,每一个Dbset相当于数据库的一张表,每一个实体相当于数据库的一行

 public class EFDbContext : DbContext
{
public EFDbContext(DbContextOptions<EFDbContext> options) : base(options)
{
} public DbSet<Admin> Admins { get; set; } public DbSet<Company> Companies { get; set; }

  可以重写DbContext的OnModelCreating来指定实体对应的数据库表名,默认数据库使用实体的复数做为表名

 protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Admins>().ToTable("Admin"); }

三、在DI注册EF上下文,Startup.cs文件里面的ConfigureServices方法里面注册EF上下文

  

 public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{ services.AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().AddJsonOptions(option =>
{
option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
}
);
}

数据库连接配置在appsettings.json文件里面:

 "ConnectionStrings": {
"DefaultConnection": "Data Source=.\\SQLExpress;Initial Catalog=DbTest;User ID=sa;Password=sa"
},

DI会在创建控制器时,通过构造函数注入EF上下文,

 public class AdminsController : SysBaseController
{
private readonly EFDbContext _context; public AdminsController(EFDbContext context)
{
_context = context;
}

  

在ASP.NET Core 2.0 web项目中使用EntityFrameworkCore的更多相关文章

  1. 我来告诉你:VS2019开发ASP.NET Core 3.0 Web项目,修改视图后,刷新浏览器看不到修改后的效果怎么处理

    VisualStudio2019下一个2.2另一个3.0页面修改如下,但是3.0刷新没有任何变化,难道VS以后不能做前端开发了?大家可能没有看官方文档 根据文章所说你需要: 1.安装 Microsof ...

  2. 从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  3. 从头编写 asp.net core 2.0 web api 基础框架 (3)

    第一部分:http://www.cnblogs.com/cgzl/p/7637250.html 第二部分:http://www.cnblogs.com/cgzl/p/7640077.html 之前我介 ...

  4. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (3)

    Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-template-from-scratc ...

  5. 【转载】从头编写 asp.net core 2.0 web api 基础框架 (1)

    工具: 1.Visual Studio 2017 V15.3.5+ 2.Postman (Chrome的App) 3.Chrome (最好是) 关于.net core或者.net core 2.0的相 ...

  6. ASP.NET Core 2.0 : 三. 项目结构

    本章我们一起来对比着ASP.NET Framework版本看一下ASP.NET Core 2.0的项目结构.(此后的文章也尽量这样对比着, 方便学习理解.) 关注差异, 也为项目迁移做准备. 新建项目 ...

  7. 从头编写asp.net core 2.0 web api 基础框架 (5) + 使用Identity Server 4建立Authorization Server (7) 可运行前后台源码

    前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...

  8. asp.net core 2.0 web api + Identity Server 4 + angular 5 可运行前后台源码

    前台使用angular 5, 后台是asp.net core 2.0 web api + identity server 4. 从头编写asp.net core 2.0 web api 基础框架: 第 ...

  9. 用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传

    第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 第二部分: http://www.cnblogs.com/cgzl/p/8481825.html 第三 ...

随机推荐

  1. python之装饰器(decorator)

    python的装饰器如果用得好,那是大神,用的不好最好别用... 装饰器(decorator)主要包含俩大属性: 1.不能改变原有函数的调用方式 2.不能改变原有函数的代码 第一个表示,我不需要改变原 ...

  2. odoo开发笔记 -- 时区问题

    odoo 时区问题 待补充 odoo默认数据库是以UTC时间存放的:这也是odoo设计优秀的地方.

  3. Python -- tabulate 模块,

    pip install tabulate >>> from tabulate import tabulate >>> table = [["Sun&quo ...

  4. dart之旅(一)

    前言 最近在看 dart 了,本着 "纸上得来终觉浅,绝知此事 markdown" 的原则,准备边学边写,写一个系列,这是第一篇.学习过程中主要是参考 A Tour of the ...

  5. Set "$USE_DEPRECATED_NDK=true" in gradle.properties to continue using the current NDK integration. 解决办法

    1.将 jni 文件夹名改为 cpp: 2.添加 CMakeLists.txt; 3.修改 build.gradle; externalNativeBuild { cmake { path " ...

  6. kdump内核转储

    目录 CentOS 7.5 配置Kdump 安装Kdump 安装Kdump图形化 配置保留内存 配置kdump类型 核心转储到本地 核心转储到设备 使用NFS指定核心转储 使用SSH指定核心转储 配置 ...

  7. 如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案

    如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案

  8. 如何发布一个npm包(基于vue)

    前言:工作的时候总是使用别人的npm包,然而我有时心底会好奇自己如何发布一个npm包呢,什么时候自己的包能够被很多人喜欢并使用呢...今天我终于迈出了第一步. 前提:会使用 npm,有 vue 基础, ...

  9. springboot+cloud 学习(三)消息中间件 RibbitMQ+Stream

    安装RabbitMQ window下安装: (1):下载erlang,原因在于RabbitMQ服务端代码是使用并发式语言erlang编写的,下载地址:http://www.erlang.org/dow ...

  10. 【转】java String.split()函数的用法分析

      在java.lang包中有String.split()方法的原型是: public String[] split(String regex, int limit) split函数是用于使用特定的切 ...