前段时间.Net Core 3.0 发布了,Entity Framework Core 3.0 也发布了Preview版。假期用了一上午大体研究了一遍,同时又体验了一把Visual Studio 2019。总结一下分享给你们:数据库
1、VS2019 新建.Net Core 3.0 Console应用,添加EFCore相关的Nuget引用json
1. 新建.Net Core控制台应用 EFCoreTestapp
新建完成后,查看项目的依赖性,咱们能够看到:ide
2. 添加Microsoft.EntityFrameworkCore 3.0 Preview版 Nuget引用函数
同时添加Microsoft.EntityFrameworkCore.SqlServer3.0 Nuget引用(咱们要用到SQL Server)ui
这样咱们就完成了项目的初始化。spa
2、增长appsettings.json配置文件,配置数据库链接3d
1. 项目中添加appsettings.json文件code
配置文件的内容以下:blog
{"ConnectionStrings": {"BizDatabase": "Server=127.0.0.1;Database=master;User id=sa;password=******" }}
2. 访问这个appsettings.json配置文件咱们须要引用如下Nuget包:版本用的都是:3.0.0-preview3.19153.1
3、新建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 = 13799990001, Controllers = new System.Collections.Generic.List<ChargeStationController> { new ChargeStationController { ID = "Station0001-101", Code = "Station0001-101", ControlAddress = "123456789", 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