.NetCore技术研究-EntityFramework Core 3.0 Preview
前段时间.Net Core 3.0 发布了,Entity Framework Core 3.0 也发布了Preview版。假期用了一上午大致研究了一遍,同时又体验了一把Visual Studio 2019。总结一下分享给大家:
- VS2019 新建.Net Core 3.0 Console应用,添加EFCore相关的Nuget引用
- 增加appSettings.json配置文件,配置数据库连接
- 新建OneToMany模型,使用EF Core完成数据库操作
一、VS2019 新建.Net Core 3.0 Console应用,添加EFCore相关的Nuget引用
1. 新建.Net Core控制台应用 EFCoreTest
新建完成后,查看项目的依赖性,我们可以看到:
2. 添加Microsoft.EntityFrameworkCore 3.0 Preview版 Nuget引用
同时添加Microsoft.EntityFrameworkCore.SqlServer3.0 Nuget引用(我们要用到SQL Server)
这样我们就完成了项目的初始化。
二、增加appsettings.json配置文件,配置数据库连接
1. 项目中添加appsettings.json文件
配置文件的内容如下:
{"ConnectionStrings": {"BizDatabase": "Server=127.0.0.1;Database=master;User id=sa;password=******" }}
2. 访问这个appsettings.json配置文件我们需要引用以下Nuget包:版本用的都是:3.0.0-preview3.19153.1
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Configuration.Json
三、新建OneToMany模型,使用EF Core完成数据库操作
这里以充电站和集控为例,1:M的关联关系。
这里我们同时使用了EF的注解,示例了个性化数据库表结构。以及外键关系
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text; namespace EFCoreTest
{
/// <summary>
/// 充电站
/// </summary>
[Table("Stations")]
public class ChargeStation
{
[Key]
[Column("ID")]
public string ID { get; set; } [Required]
[Column("Code")]
public string Code { get; set; } [Required]
[Column("Name")]
public string Name { get; set; } [Required]
[Column("MaintainTel")]
public long MaintainTel { get; set; } [ForeignKey("StationID")]
public virtual List<ChargeStationController> Controllers { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text; namespace EFCoreTest
{
/// <summary>
/// 电站集控
/// </summary>
[Table("StationCtrl")]
public class ChargeStationController
{
[Key]
[Column("ID")]
public string ID { get; set; } [Required]
[Column("Code")]
public string Code { get; set; } [Required]
[Column("ControlAddress")]
public string ControlAddress { get; set; } [Required]
[Column("StationID")]
[ForeignKey("StationID")]
public string StationID { get; set; }
}
}
实体及关联关系搞定后,我们介绍今天的主角 DbContext的实现:ChargeDbContext
ChargeDbContext 有几个重要的属性和方法:
public DbSet<ChargeStation> Stations { get; set; }
public DbSet<ChargeStationController> StationCtrl { get; set; }
重载OnConfiguring方法,加载配置系统、数据库连接串
public class ChargeDbContext : DbContext
{
public DbSet<ChargeStation> Stations { get; set; }
public DbSet<ChargeStationController> StationCtrl { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json"); var configuration = builder.Build(); var conn = configuration.GetConnectionString("BizDatabase");
optionsBuilder.UseSqlServer(conn);
}
}
至此, 核心主要的EFCore 代码已经完成,我们继续来实现对ChargeStation的数据库操作:
我们在Main函数中实现对ChargeStation和ChargeStationController的删除和保存操作,以下代码,大家用过EF应该很熟悉:
class Program
{
static void Main(string[] args)
{
using (var context = new ChargeDbContext())
{
foreach (var sta in context.Stations.Include(i => i.Controllers))
{
context.Remove(sta);
} context.SaveChanges(); var station = new ChargeStation
{
ID = "Station0001",
Code = "Station0001",
Name = "济南市奥体中路汉庭充电站",
MaintainTel = ,
Controllers = new System.Collections.Generic.List<ChargeStationController>
{
new ChargeStationController
{
ID = "Station0001-101",
Code = "Station0001-101",
ControlAddress = "",
StationID = "Station0001"
}
}
}; context.Stations.Add(station);
context.SaveChanges();
Console.WriteLine("Press any key!");
Console.ReadKey();
}
}
}
以上即是EntityFramework Core 3.0 Preview 体验和使用分享,后续有会有一篇文章介绍在Debug时,如何Trace SQL语句。
周国庆
2019/4/6
.NetCore技术研究-EntityFramework Core 3.0 Preview的更多相关文章
- .NetCore技术研究-.NET Core迁移前的准备工作
前段时间迁移.NET Core做了大量的试水和评估,今天整理一下分享给大家.大致有以下几个部分: 1. .NET Core的由来 2. 为什么要迁移.NET Core 3. .NET Core3.X主 ...
- 【译】.NET Core 3.0 Preview 3中关于ASP.NET Core的更新内容
.NET Core 3.0 Preview 3已经推出,它包含了一系列关于ASP.NET Core的新的更新. 下面是该预览版的更新列表: Razor组件改进: 单项目模板 新的Razer扩展 E ...
- asp.net core 1.1 项目升级至 asp.net core 2.0 preview 2 与正式版
这两天把一个 asp.net core 1.1 的项目迁移到了 asp.net core 2.0 preview 2 ,在这篇随笔中记录一下. 如果项目在有 global.json 文件,需要删除或修 ...
- 用VS Code体验调试.NET Core 2.0 Preview (传统三层架构)
准备工作 VS Code下载地址:https://vscode.cdn.azure.cn/stable/379d2efb5539b09112c793d3d9a413017d736f89/VSCodeS ...
- EntityFramework Core 2.0执行原始查询如何防止SQL注入?
前言 接下来一段时间我们来讲讲EntityFramework Core基础,精简的内容,深入浅出,希望为想学习EntityFramework Core的童鞋提供一点帮助. EntityFramewor ...
- EntityFramework Core 2.0全局过滤(HasQueryFilter)
前言 EntityFramework Core每一次版本的迭代和更新都会带给我们惊喜,每次都会尽量满足大部分使用者的需求.在EF Core 2.0版本中出现了全局过滤新特性即HasQueryFilte ...
- EntityFramework Core 2.0 Explicitly Compiled Query(显式编译查询)
前言 EntityFramework Core 2.0引入了显式编译查询,在查询数据时预先编译好LINQ查询便于在请求数据时能够立即响应.显式编译查询提供了高可用场景,通过使用显式编译的查询可以提高查 ...
- selenium相关技术研究(从1.0-3.0)
注: 以下内容引自http://www.cnblogs.com/hhudaqiang/p/6550135.html Selenium相关技术研究(从1.0-3.0) 好吧,最近看wxpython有点多 ...
- .NET Core 2.0 Preview 1发布下载和文档
.NET Core 2.0.0 Preview 1 发布于 2017 5.10. 你可以通过 Visual Studio 2017 Preview 15.3, Visual Studio for Ma ...
随机推荐
- DAPP 开发直通车-如何基于NEL 轻钱包来开发DAPP
之前做了 DAPP 开发直通车,通讲了一下开发一个DAPP的过程. 但是涉及多工种,多步骤.入手还是非常困难的. 经过不懈的努力,做了很多铺垫工作之后,我终于可以告诉你: 开发DAPP f ...
- __x__(40)0909第五天__表格 table 的 css 样式 美化
如果就向下面的代码那样,不写 tbody , 则浏览器自添加 tbody , 并将所有的 tr 移入 tbody 意味着 tr 并非 table 的子元素,而是 tbody 的子元素. 所以 以后编写 ...
- JS-函数声明 和 函数表达式
问题: 1, function foo() {}; 2, var foo = function () {}; 1,上面的语法是声明,可以提升,因此在函数定义的上方也可以调用 2,下面的语法是函数表达式 ...
- HTTP学习总结
首先是一张总结的图: 对各个不同的通信进行解分: 1.http通信详解 2.cookie通信图: 3.cookie管理的session信息 4.token通信
- 常用的当前时间(返回String类型)
public class TimeUtil { /** * 创建人:zhiyuan * 创建时间:2018年6月9日上午11:31:02 * 方法描述:以yyyy-MM-dd查询当前时间 */ pub ...
- c++库函数 Map
转载:https://blog.csdn.net/shuzfan/article/details/53115922 C++中map提供的是一种键值对容器,里面的数据都是成对出现的,如下图:每一对中的第 ...
- springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、
流程: 1.用户调登录接口,传用户名和密码2.用户名和密码在ad验证,验证通过后,返回当前用户的相关信息.(注:ldap为java自带的api不需要maven引入其他的)3.根据返回的用户信息,实现自 ...
- X86-32位架构的CPU是不是内存只能到4G
不是的,可以通过分页机制扩展实现超过4G内存的支持. 什么是分页机制扩展? PAE. 什么是PAE? PAE如何实现的?
- javascript 表达式
// for(表达式1;表达式2;表达式3){// 循环体语句;// }// 先执行表达式1,在执行2表达式,// 如果2表达式结果为false,退出循环 ...
- Gitbook在Windows上安装
GitBook是基于Nodejs,使用Git/Github和Markdown制作电子书的命令行工具. 1.安装Nodejs 首先,安装Nodejs,官网地址:https://nodejs.org/en ...