EFCore入门建表,增删改查
安装两个NuGet:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
1.创建一个Book.cs文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { public class Book { public long Id { get; set; } //主键 public string Title { get; set; } //标题 public DateTime PubTime { get; set; } //发布日期 public double Price { get; set; } //单价 public string AuthorName { get; set; } //作者 } }
2.创建一个配置Book.cs文件的BookConfig.cs文件,如果进行配置,也可不创建
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { public class BookConfig : IEntityTypeConfiguration<Book> { public void Configure(EntityTypeBuilder<Book> builder) { builder.ToTable("T-Books"); //设置 Book和那一个表对应 builder.Property(x => x.Title).HasMaxLength(50).IsRequired(); //IsRequired不能为空 //HasMaxLength最大长度 builder.Property(s=>s.AuthorName).HasMaxLength(20).IsRequired(); //IsRequired不能为空 //HasMaxLength最大长度 builder.Property(x => x.Name).HasColumnType("varchar(20)"); //HasColumnType指定数据类型 //修改其它属性作为主键(默认Id作为主键使用) //builder.HasKey(x => x.Price); //设置默认值 builder.Property(x => x.PubTime).HasDefaultValue(DateTime.Now); //设置唯一索引 builder.HasIndex(x => x.Title).IsUnique(); //设置符合索引 builder.HasIndex(x => new { x.Name, x.AuthorName }); } } }
3,自定义一个文件,我这点取名MyDbContext.cs
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { public class MyDbContext: DbContext { public DbSet<Book> books { get; set; } //public DbSet<Person> persons { get; set; } //public DbSet<Dog> Dogs { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.UseSqlServer("server=.;database=PerformanceAppraisalDb;uid=sa;pwd=123456;Encrypt=false;"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //从当前程序集里面加载所以的IEntityTypeConfiguration modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly); } } }
4. 在程序包管理器控制台输入
Add-Migration 此处自定义说明添加描述
举例: Add-Migration Init
Update-Database 更新到数据库
5.查询数据,插入数据
static void Main() { using (MyDbContext ctx = new MyDbContext()) { //向数据库插入数据 //Book book = new Book(); //book.Title = "标题2"; //book.Price = 23.00; //book.AuthorName = "作者名字5"; //book.PubTime = DateTime.Now; //ctx.books.Add(book); // 添加到Books逻辑表里面 //ctx.SaveChanges(); 保存,同步到数据库 //查询数据 var items = ctx.books.Where(m => m.Price >= 50); foreach (var item in items) { Console.WriteLine(item.Price); } var s = ctx.books.OrderBy(m => m.Price); foreach (var item in s) { Console.WriteLine(item.Price); } Console.ReadKey(); } }
6. 编辑数据,删除数据
//修改数据,删除数据 var b = ctx.books.Single(i => i.Id == 1); b.Title = "新标题1"; var d = ctx.books.Single(n=>n.Id == 8); ctx.books.Remove(d); ctx.SaveChanges();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?