前段時間.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