前段时间. 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 = 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
来源: https://www.cnblogs.com/tianqing/p/10661571.html