一:反向工程:
根据已存在的数据库对应的表 反向建立实体类 (DBFirst)
1.安装Nuget
Install-Package Microsoft.EntityFrameworkCore.Tools
2.运行命令:
Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer
例子:
PM>Scaffold-DbContext "Server = .;Database=Demo;UserId=sa;Password=admin123;Trusted_Connection=True;MultipleActiveResultSets=true" Microsoft.EntityFrameworkCore.SqlServer
二:EF底层原理
EF主要是把C#代码生成SQL语句,然后交由ADO.NET Core,由ADO.NET去数据库执行
三:查看EF生成的SQL
新建控制台项目
--Part
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EF_03 { /// <summary> /// 零件 /// </summary> internal class Part { public int Id { get; set; } /// <summary> /// 零件名称 /// </summary> public string PartName { get; set; } /// <summary> /// 零件颜色 /// </summary> public string PartColor { get; set; } /// <summary> /// 数量 /// </summary> public int GoodsNumber { get; set; } /// <summary> /// 厂家 /// </summary> public string Manufacturer { get; set; } } }
--PartConfig.CS
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; namespace EF_03 { internal class PartConfig : IEntityTypeConfiguration<Part> { public void Configure(EntityTypeBuilder<Part> builder) { builder.Property(x => x.PartName).HasMaxLength(150); builder.Property(x => x.Manufacturer).HasMaxLength(500); builder.Property(x => x.PartColor).HasMaxLength(60); } } }
--DemoDBcontext.cs(记录SQL)
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace EF_03 { internal class DemoDBcontext : DbContext { public DbSet<Part> Parts { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); string ConStr = "Server = .; Database = Demo; User Id = sa; Password = admin123;connection timeout=600"; optionsBuilder.UseSqlServer(ConStr); optionsBuilder.LogTo(Console.WriteLine); //增加SQL日志记录 } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } } }
Program
using EF_03; //初始化 List<Part> parts = new List<Part>() { new Part(){ PartName="扳手",PartColor="银色",GoodsNumber=2,Manufacturer="施贝德智慧科技有限公司"}, new Part(){ PartName="锤子",PartColor="绿色",GoodsNumber=1,Manufacturer="中山市小榄镇尼奥金属制品厂"}, new Part(){ PartName="铁锹",PartColor="灰色",GoodsNumber=5,Manufacturer="重庆佳装环保科技有限公司"}, new Part(){ PartName="套筒",PartColor="银色",GoodsNumber=20,Manufacturer="重庆佳装环保科技有限公司"}, new Part(){ PartName="大连杆",PartColor="银色",GoodsNumber=5,Manufacturer="中山市小榄镇尼奥金属制品厂"}, new Part(){ PartName="内六角",PartColor="银色",GoodsNumber=8,Manufacturer="苏州质品紧固件有限公司"}, new Part(){ PartName="内五角",PartColor="银色",GoodsNumber=6,Manufacturer="施贝德智慧科技有限公司"}, new Part(){ PartName="内四角",PartColor="银色",GoodsNumber=5,Manufacturer="苏州质品紧固件有限公司"}, new Part(){ PartName="内三角",PartColor="银色",GoodsNumber=10,Manufacturer="施贝德智慧科技有限公司"}, new Part(){ PartName="两级",PartColor="金色",GoodsNumber=200,Manufacturer="深圳市振立辉仓储科技有限公司"} }; //数据库操作 using (DemoDBcontext dbcontext = new DemoDBcontext()) { //写入 await dbcontext.AddRangeAsync(parts); await dbcontext.SaveChangesAsync(); //查询: Part? part= dbcontext.Parts.FirstOrDefault(); if (part!=null) { Console.WriteLine($"查询到零件{part.PartName},{part.PartColor},一共{part.GoodsNumber}个,来自{part.Manufacturer}生产"); } else { Console.WriteLine("未获取到零件"); } } Console.WriteLine("Done.."); Console.ReadKey();
执行Add-Migration和Update-database 生成表
然后运行看结果:
新增的SQL:
查询的SQL
2.查询的方法可以使用ToQueryString();
修改Program:
using (DemoDBcontext dbcontext = new DemoDBcontext()) { //查询: string sql= dbcontext.Parts.Where(x=>x.PartName== "内四角").ToQueryString(); Console.WriteLine(sql); } Console.WriteLine("Done.."); Console.ReadKey();
再来看结果: