首先安装Nuget包

Install-package Microsoft.EntityFrameworkCore
Install-package Microsoft.EntityFrameworkCore.SqlServer

Micorsoft.EntityFrameworkCore:EF框架的核心包
Micorsoft.EntityFrameworkCore.SqlServer:针对SqlServer数据库的扩展

其次设置(appsettings.json)配置文件的数据连接字符串

"ConnectionStrings": {
"SqlServer": "Data Source=localhost;Initial Catalog=testingdb;Integrated Security=False;Persist Security Info=False;User ID=sa;Password=password"
}

创建实体(province)

namespace MicroCore
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
[Table("dt_province")]
public class province
{
[Key]
/// <summary>
/// 自动
/// </summary>
public int id { set; get; }
/// <summary>
/// 当前标识
/// </summary>
public string code { set; get; }
/// <summary>
/// 名称
/// </summary>
public string name { set; get; }
}
}

    

方法一、通过配置链接数据库

1、创建Entity Framework Core配置

namespace MicroCore
{
using Microsoft.EntityFrameworkCore;
public class EFContext : DbContext
{
public EFContext(DbContextOptions<EFContext> options) : base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<province>(entity =>
{
entity.ToTable("dt_province");
entity.HasKey(a => a.id);
});
}
public DbSet<province> province { get; set; }
}
}

2、Startup 启动注册链接

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddApplicationInsightsTelemetry(Configuration);
services.AddEntityFrameworkSqlServer().AddDbContext<EFContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
}

3、Index.cshtml.cs调用数据

namespace MicroCore.Pages
{
public class IndexModel : PageModel
{
private readonly EFContext db;
public IndexModel(EFContext db)
{
this.db = db;
} public void OnGet()
{
var result = db.province.Where(p => p.id == 1).ToList();
}
}
}

  

方法二、自定义配置链接数据库

1、创建Entity Framework Core配置

namespace MicroCore
{
using Microsoft.EntityFrameworkCore;
public class EFContext : DbContext
{
public static string ConnectionString { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(ConnectionString, b => b.UseRowNumberForPaging());
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<province>(entity =>
{
entity.ToTable("dt_province");
entity.HasKey(a => a.id);
});
}
public DbSet<province> province { get; set; }
}
}

2、Startup 取得配置链接

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
EFContext.ConnectionString = Configuration.GetConnectionString("SqlServer");
}

3、Index.cshtml.cs调用数据

namespace MicroCore.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{
EFContext db = new EFContext();
var result = db.province.Where(p => p.id == 3).ToList();
}
}
}

  

  本文源码下载    

  

.NET Core Entity使用Entity Framework Core链接数据库的更多相关文章

  1. Entity Framework Core 1.1 升级通告

    原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...

  2. UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?

    选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...

  3. Entity Framework Core 1.1 Preview 1 简介

    实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...

  4. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 更新关系数据

    Updating related data¶ 7 of 7 people found this helpful The Contoso University sample web applicatio ...

  5. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 读取关系数据

    Reading related data¶ 9 of 9 people found this helpful The Contoso University sample web application ...

  6. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio »迁移

    Migrations¶ 4 of 4 people found this helpful The Contoso University sample web application demonstra ...

  7. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型

    Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...

  8. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 排序、筛选、分页以及分组

    Sorting, filtering, paging, and grouping 7 of 8 people found this helpful By Tom Dykstra The Contoso ...

  9. Entity Framework Core 命名约定

    本文翻译自<Entity Framework Core: Naming Convention>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意:我使用的是 Entity ...

随机推荐

  1. 性能测试五:jmeter进阶之后置处理器(正则、json提取器)

    如,从get返回的json中提取stock的值 作为post的请求参数 1.JSON提取器 专门对json数据进行提取的后置处理器 Debug Sampler:记录之前的请求的所有参数及数据 2.正则 ...

  2. Python 检测系统时间,k8s版本,redis集群,etcd,mysql,ceph,kafka

    一.概述 线上有一套k8s集群,部署了很多应用.现在需要对一些基础服务做一些常规检测,比如: 系统时间,要求:k8s的每一个节点的时间,差值上下不超过2秒 k8s版本,要求:k8s的每一个节点的版本必 ...

  3. 浅析微信支付:微信支付简单介绍(小程序、公众号、App、H5)

    本文是[浅析微信支付]系列文章的第二篇,主要讲解一下普通商户接入的支付方式以及其中的不同之处. 上篇文章讲了本系列的大纲,没有看过的朋友们可以看一下. 浅析微信支付:前篇大纲 微信支付是集成在微信客户 ...

  4. Python 打印当前文件相对路径和绝对路径

    一.打印相对路径 print(__file__) 二.打印绝对路径 import os print(os.path.abspath(__file__)) 三.打印文件名 import os print ...

  5. Spring日记_01 之基于maven的Spring环境搭建

    阿里云镜像:maven.aliyun.com 添加Spring坐标: Spring 是java组件容器,Java饭馆 使用者可以通过getBean(对象ID) 获得Date对象,而不需要自己去new ...

  6. 文件流 io.StringIo()

    import io f = io.StringIO() f.write("") f.getvalue() f.close 二进制 f = io.Bytesio()

  7. Web轻量级扫描工具Skipfish

    Web轻量级扫描工具Skipfish 1. Skipfish 简介 2. Skipfish 基本操作 3.身份认证 一. Skipfish 简介 Skipfish是一款主动的Web应用程序安全侦察工具 ...

  8. Nuxt.js 如何在 asyncData中 请求数据 ,并将拿到的数据传给子组件

    说明:同接口请求一样,也可以进行数据的处理:return  中 左侧的变量  可以直接拿到在页面上使用,也可以传递给子组件 下面再给出一段代码,方便觉得有用的.却又不想手敲的朋友们: async as ...

  9. 运营商DNS系统安全解决方案

    DNS系统面临的主要风险 目前,DNS面临的安全问题主要可以分为三类:DNS欺骗攻击.拒绝服务攻击.系统漏洞,下文将分别进行介绍.  DNS欺骗攻击 当一个DNS服务器遭到欺骗攻击,使用了来自一个恶 ...

  10. BZOJ.2879.[NOI2012]美食节(费用流SPFA)

    题目链接 /* 同"修车":对于每个厨师拆成p个点表示p个时间点,每个人向m个厨师每个时间点连边 这样边数O(nmp)+网络流 ≈O(nm*p^2)(假设SPFA线性) = GG ...