首先安装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. Date对象和Time对象

    方法 描述  getDate()  : setDate()  返回/设置月份中的日期(1-31)  getDay()  返回星期几(0-6)  getFullYear():setFullYear() ...

  2. 检查Rabbitmq中队列及消息个数,还有清空的方法

    rabbitmqctl list_queues rabbitmqctl purge_queue kafka_queue (不推荐)使用reset方式重置rabbitmq即清空掉rabbitmq所有的队 ...

  3. usaco 校园网

    题解: 显然当一个图上的点是一个环时能满足题目要求 那么我们来考虑怎么形成一个环 很显然的是要先缩点 缩完点就成为了森林,如何让森林成环呢? 考虑一下环上的点的入度出度一定都大于1 而连一条边可以增加 ...

  4. 使用sshtunnel实现python公网连接阿里云mongo服务器

    背景: 公司使用阿里云的云数据库MongoDB.基于安全原因考虑,阿里云MongoDB云数据库目前只支持从阿里云ECS上访问,无法通过公网直接访问,不方便用户在本地开发环境里直接进行测试. 阿里云官方 ...

  5. will-change属性

    牛逼的 will-change属性 will-change属性可以提前通知浏览器我们要对元素做什么动画,这样浏览器可以提前准备合适的优化设置.这样可以避免对页面响应速度有重要影响的昂贵成本.元素可以更 ...

  6. HDU 2602 Bone Collector 骨头收集者【01背包】

    题目链接:https://vjudge.net/contest/103424#problem/A 题目大意: 第一行输入几组数据,第二行第一个数字代表物体个数,第二个数代表总体积.需要注意的是,第三排 ...

  7. C#并行编程(3):并行循环

    初识并行循环 并行循环主要用来处理数据并行的,如,同时对数组或列表中的多个数据执行相同的操作. 在C#编程中,我们使用并行类System.Threading.Tasks.Parallel提供的静态方法 ...

  8. Java设计模式从精通到入门三 策略模式

    介绍 我尽量用最少的语言解释总结: Java23种设计模式之一,属于行为型模式.一个类的行为或者算法可以在运行时更改,策略对象改变context对象执行算法. 应用实例: ​ 以周瑜赔了夫人又折兵的例 ...

  9. JavaScript学习总结(六)——前端模块化开发

    早期的javascript版本没有块级作用域.没有类.没有包.也没有模块,这样会带来一些问题,如复用.依赖.冲突.代码组织混乱等,随着前端的膨胀,模块化显得非常迫切. 前端模块化规范如下: 一.前端模 ...

  10. Linux虚拟机上安装redis

    1.检查安装依赖程序 yum install gcc-c++ yum install -y tcl yum install wget 2.获取安装文件 wget http://download.red ...