参考自:https://www.cnblogs.com/heheblog/p/net_study_20180902.html
1、添加核心程序包
Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore
2、添加实体类
public partial class JobDetail { public long Id { get; set; } /// <summary> /// job名称 /// </summary> [Comment("job名称")] [MaxLength(64)] public string JobName { get; set; } = null!; /// <summary> /// job分组 /// comment用于注释 /// </summary> [Comment("job分组")] [MaxLength(64)] public string JobGroup { get; set; } = null!; public int JobStatus { get; set; } [MaxLength(256)] public string JobDescription { get; set; } = null!; [MaxLength(64)] public string JobAssembly { get; set; } = null!; /// <summary> /// job参数 /// 这里不设置最大长度,表明用nvarchar(max) /// </summary> public string JobData { get; set; } = null!; [MaxLength(16)] public string CronExpression { get; set; } = null!; [DataType(DataType.DateTime)] public DateTime CreateTime { get; set; } }
3、添加DbContext(这个DbContext是用Scaffold-DbContext自动生成的,将就用)
public partial class QuartzAutoJobDBContext : DbContext { public QuartzAutoJobDBContext() { } public QuartzAutoJobDBContext(DbContextOptions<QuartzAutoJobDBContext> options) : base(options) { } public virtual DbSet<JobDetail> JobDetails { get; set; } = null!; public virtual DbSet<JobExecuteResult> JobExecuteResults { get; set; } = null!; public virtual DbSet<JobExecuteResultLog> JobExecuteResultLogs { get; set; } = null!; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (!optionsBuilder.IsConfigured) { #warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263. optionsBuilder.UseSqlServer("Server=.;Database=QuartzAutoJobDB;uid=sa;pwd=123456"); } } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<JobDetail>(entity => { entity.ToTable("JobDetail"); entity.Property(e => e.CreateTime) .HasColumnType("datetime") .HasDefaultValueSql("(getdate())"); entity.Property(e => e.CronExpression).HasMaxLength(32); entity.Property(e => e.JobAssembly).HasMaxLength(256); entity.Property(e => e.JobDescription).HasMaxLength(256); entity.Property(e => e.JobGroup).HasMaxLength(64); entity.Property(e => e.JobName).HasMaxLength(64); }); modelBuilder.Entity<JobExecuteResult>(entity => { entity.ToTable("JobExecuteResult"); entity.Property(e => e.CreateTime) .HasColumnType("datetime") .HasDefaultValueSql("(getdate())"); entity.Property(e => e.JobGroup).HasMaxLength(64); entity.Property(e => e.JobName).HasMaxLength(64); entity.Property(e => e.TrackId).HasMaxLength(1); }); modelBuilder.Entity<JobExecuteResultLog>(entity => { entity.ToTable("JobExecuteResultLog"); entity.Property(e => e.CreateTime) .HasColumnType("datetime") .HasDefaultValueSql("(getdate())"); entity.Property(e => e.TrackId).HasMaxLength(64); entity.Property(e => e.UpdateTime) .HasColumnType("datetime") .HasDefaultValueSql("(getdate())"); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); }
4、更新表结构(注意:别莽楚楚的乱用到生产环境)
using (QuartzAutoJobDBContext db = new QuartzAutoJobDBContext()) { db.Database.EnsureDeleted();//先删除 db.Database.EnsureCreated();//后添加 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)