.Net Core EFCore CodeFirst

参考自: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();//后添加
}


原文地址:https://www.cnblogs.com/jianghaidong/p/15673134.html