.NetCore技术研究-EntityFramework Core 3.0 Preview

时间:2023-03-09 10:00:19
.NetCore技术研究-EntityFramework Core 3.0 Preview

前段时间.Net Core 3.0 发布了,Entity Framework Core 3.0 也发布了Preview版。假期用了一上午大致研究了一遍,同时又体验了一把Visual Studio 2019。总结一下分享给大家:

  1. VS2019 新建.Net Core 3.0 Console应用,添加EFCore相关的Nuget引用
  2. 增加appSettings.json配置文件,配置数据库连接
  3. 新建OneToMany模型,使用EF Core完成数据库操作

一、VS2019 新建.Net Core 3.0 Console应用,添加EFCore相关的Nuget引用

 1. 新建.Net Core控制台应用 EFCoreTest

.NetCore技术研究-EntityFramework Core 3.0 Preview

.NetCore技术研究-EntityFramework Core 3.0 Preview

新建完成后,查看项目的依赖性,我们可以看到:

.NetCore技术研究-EntityFramework Core 3.0 Preview

2. 添加Microsoft.EntityFrameworkCore 3.0 Preview版 Nuget引用

.NetCore技术研究-EntityFramework Core 3.0 Preview

同时添加Microsoft.EntityFrameworkCore.SqlServer3.0 Nuget引用(我们要用到SQL Server)

.NetCore技术研究-EntityFramework Core 3.0 Preview

这样我们就完成了项目的初始化。

二、增加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