.NET Core Entity使用Entity Framework Core链接数据库
首先安装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链接数据库的更多相关文章
- Entity Framework Core 1.1 升级通告
原文地址:https://blogs.msdn.microsoft.com/dotnet/2016/11/16/announcing-entity-framework-core-1-1/ 翻译:杨晓东 ...
- UWP开发之ORM实践:如何使用Entity Framework Core做SQLite数据持久层?
选择SQLite的理由 在做UWP开发的时候我们首选的本地数据库一般都是Sqlite,我以前也不知道为啥?后来仔细研究了一下也是有原因的: 1,微软做的UWP应用大部分也是用Sqlite.或者说是微软 ...
- Entity Framework Core 1.1 Preview 1 简介
实体框架核心(EF Core)是Entity Framework的一个轻量级,可扩展和跨平台版本. 10月25日,Entity Framework Core 1.1 Preview 1发布了. 升级到 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Entity Framework Core 命名约定
本文翻译自<Entity Framework Core: Naming Convention>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意:我使用的是 Entity ...
随机推荐
- 算法-----python实现
斐波那契数列 def f(n): if n == 1: return 1 elif n == 2: return 1 else: return f(n-1)+f(n-2) print(f(8)) 用普 ...
- python 全栈开发,Day76(Django组件-cookie,session)
昨日内容回顾 1 json 轻量级的数据交换格式 在python 序列化方法:json.dumps() 反序列化方法:json.loads() 在JS中: 序列化方法:JSON.stringfy() ...
- java12小时制的时间转换为24小时制
Java中将12小时制的时间转换为24小时制的方式如下: import java.text.SimpleDateFormat; import java.util.Date; public class ...
- python全栈开发day34-线程Thread
一.昨日内容回顾 1. 概念和理论 进程是计算机资源分配最小单位 进程三状态.同步.异步.阻塞.非阻塞 2. 进程的创建 实例化.自建类run,start,join,terminate,daemon等 ...
- 使用Ztree新增角色和编辑角色回显
最近在项目中使用到了ztree,在回显时候费了点时间,特记录下来供下次参考. 1.新增角色使用ztree加载权限,由于权限不多,所以使用直接全部加载. 效果图: 具体涉及ztree代码: jsp中导入 ...
- Vijos1906 联合权值 NOIP2014Day1T2 树形动态规划
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - Vijos1906 题意概括 有一棵树,每一个节点都有一个权值w[i].下面说的x,y都是该树中的节点. 对于 ...
- 6-16 单词 uva10129
了解了欧拉回路和欧拉道路的性质 : 欧拉道路 最多只有两个奇点(不可能只有一个奇点) 并且当有两个奇点的时候 一个奇点入比出多一 一个奇点出比入多一 采用并查集查看是否连同 如果连 ...
- C++ 的static 与 const
1.static成员变量(非const)必须在类外定义,在类中只是作为声明(声明其scope为该类),不能使用类初始化成员列表来初始化,只能在定义的时候初始化. 2.static const的成员变量 ...
- hdu 2063 过山车【匈牙利算法】(经典)
<题目链接> RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partne ...
- Python中 各种数字类型的判别(numerica, digital, decimal)
一. 全角和半角 全角:是指一个全角字符占用两个标准字符(或两个半角字符)的位置. 全角占两个字节. 汉字字符和规定了全角的英文字符及国标GB2312-80中的图形符号和特殊字符都是全角字符.在全角中 ...