.net core EF introductory notes

Recently wrote a background task polling project, understanding down the final choice quartZ, since it is a project, the first is a database access layer, .net core has been out to 3.0, and white, I also want to try, or personal like ORM framework, talking about Microsoft's ORM framework should first think of the EntityFramework, OK then let me up entry bar, where I will start with .net core console console application to start.

Since it is ORM framework, we should first have Model (code first uses this mode), normal operation after a meal

 

 Our classes are as follows:

 

 public class QuartJobLog
    {
        private int _id = 0;
        private string _job_name;
        private string _job_result;
        private string _job_exception;
        private int _job_exetime;
        private DateTime _job_exedate;
        private int _job_exestate;
        private DateTime _addtime;

        /// <summary>
        /// 主键
        /// </summary>
        public int Id { get => _id; set => _id = value; }
        /// <summary>
        /// 
        /// </summary>
        public int JobId { get; set; }
        /// <summary>
        /// 任务名称
        /// </summary>
        public string Job_name { get => _job_name; set => _job_name = value; }
        /// <summary>
        /// 执行的结果
        /// </summary>
        public string Job_result { get => _job_result; SET => _job_result = value;}
         ///  <Summary> 
        /// abnormality information to perform tasks
         ///  </ Summary> 
        public  String Job_exception { GET => _job_exception; SET => _job_exception = value;}
         ///  < Summary> 
        /// time-consuming unit MS
         ///  </ Summary> 
        public  int Job_exetime { GET => _job_exetime; SET => _job_exetime = value;}
         ///  <Summary> 
        /// date and time of execution of the tasks
         ///  </ Summary> 
        public{Job_exedate the DateTime GET => _job_exedate; SET => _job_exedate = value;}
         ///  <Summary> 
        /// execution result normal 0, abnormal 1
         ///  </ Summary> 
        public  int Job_exestate { GET => _job_exestate; SET = > _job_exestate = value;}
         ///  <Summary> 
        /// task execution time
         ///  </ Summary> 
        public the DateTime Addtime { GET => _addtime; SET => _addtime = value;} 
    }

 

 

public class QuartJobSchedule
    {
        private int _id = 0;
        private string _job_name;
        private string _job_assembly;
        private string _job_class;
        private string _job_corn;
        private int _job_type = 1;
        private int _job_execount = 0;
        private DateTime _job_starttime;
        private int _job_state = 0;
        private DateTime _addtime;

        /// <summary>
        /// 主键
        /// </summary>
        public int Id { get => _id; set => _id = value; }
        /// <summary>
        /// 任务名称
        /// </summary>
        public string Job_Name { get => _job_name; set => _job_name = value; }
        /// <summary>
        /// 执行的方式的dll名称
        /// </summary>
        public string Job_Assembly { get=> _Job_assembly; SET => _job_assembly = value;}
         ///  <Summary> 
        /// method of performing class
         ///  </ Summary> 
        public  String Job_Class { GET => _job_class; SET => _job_class = value;}
         / //  <Summary> 
        /// mission corn expression
         ///  </ Summary> 
        public  String Job_Corn { GET => _job_corn; SET => _job_corn = value;}
         ///  <Summary> 
        /// task type, The default is a simple, 2 complex
         /// </ Summary> 
        public  int JOB_TYPE { GET => _job_type; SET => _job_type = value;}
         ///  <Summary> 
        /// perform the total number of tasks, 0 indicates unlimited
         ///  </ Summary> 
        public  int Job_Execount { GET => _job_execount; SET => _job_execount = value;}
         ///  <Summary> 
        /// task start time
         ///  </ Summary> 
        public the DateTime Job_Starttime { GET => _job_starttime; SET => _job_starttime = value;}
         /// <Summary> 
        /// task preparation state 0, 1 performed, the tentative 2, 3 is stopped, ending. 4
         ///  </ Summary> 
        public  int Job_State { GET => _job_state; SET => _job_state = value;}
         ///  <Summary> 
        /// creation time task
         ///  </ Summary> 
        public the DateTime AddTime { GET => _addtime; SET => _addtime = value;} 
    }

 Next, we continue to add context object

 public class DataDBContext : DbContext
    {
        //public DataDBContext(DbContextOptions<DataDBContext> options)
        //   : base(options)
        //{ }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql("server=.;database=*****;User ID=***;Password=****");
        }


        public DbSet<QuartJobLog> QuartJobLogs { get; set; }
        public DbSet<QuartJobSchedule> QuartJobSchedules { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<QuartJobLog>(entity => {

                entity.ToTable("QuartJobLog");
                entity.HasKey(e => e.Id);
            });


            modelBuilder.Entity<QuartJobSchedule>(entity => {

                entity.ToTable("QuartJobSchedule");
                entity.HasKey(e => e.Id);
            });

           
        } 

    }

 After the next step is the migration of the database, here we import Pomelo.EntityFrameworkCore.MySql, Microsoft.EntityFrameworkCore.Tools with nuget management tools, such as VS operation is completed, we nuget package management control program input

Migration command, 1.add-migration First_Migration, 2.Update-Database select the corresponding item, and if the class library, then choose the class library project

 

 After successfully generating the corresponding table in the database there is a

 

Guess you like

Origin www.cnblogs.com/Xty09/p/12492000.html