EFCore自己用的点东西

测试一下EF core 的操作 蛀牙总是用导航属性有的时候有点不理解 写个例子给自己用

class Program
    {
        static void Main(string[] args)
        {
            MyDbContext myDbContext = new MyDbContext();
           

            try
            {
                #region 添加业务/第一种添加 student中把导航属性school也加进去 这是新的用法
                //AddWithDaoHangShuXing(myDbContext);
                #endregion

                #region 添加业务/第二种添加 先添加student 添加后再添加school
                //AddWithSingle(myDbContext);
                #endregion

                #region 修改业务 按照通俗的业务逻辑 那么修改传回来的就是类型本身 即所有的属性都可以更改
                //1.首先 查出来所有的学生数据
                //1.1 lambada表达式
                var students = myDbContext.Student.ToList();
                var studentWithDaoHangShuXings = myDbContext.Student.Include(x => x.School).ToList();
                var studentWithDaoHangShuXingProvinceces = myDbContext.Student.Include(x => x.School).ThenInclude(x=>x.Province).ToList();
                //1.2 linq
                var studentLinqs = (from s in myDbContext.Student 
                                    select new { 
                                        s.StudentId, s.StudentAge, s.StudentName, s.School 
                                    }).ToList();
                                   #endregion

                Console.WriteLine("操作完毕!");
            }
            catch (Exception ex)
            {

            }
            Console.ReadLine();
        }

        private static void AddWithSingle(MyDbContext myDbContext)
        {
            Student student = new Student
            {
                StudentName = "张汶莱",
                StudentAge = 26
            };
            myDbContext.Student.Add(student);
            myDbContext.SaveChanges();

            School school = new School
            {
                StudentId = student.StudentId,
                SchAddress = "广发学校",
                SchName = "广发学校地址"
            };
            myDbContext.School.Add(school);
            myDbContext.SaveChanges();
        }

        private static void AddWithDaoHangShuXing(MyDbContext myDbContext)
        {
            Province province = new Province
            {
                ProvinceLeaderName = "党*",
                ProvinceName = "陕西省"
            };

            School school = new School
            {
                SchAddress = "老毛学校地址",
                SchName = "老毛小学",
                SchoolId = 0,
                Province = province
            };
            Student student = new Student
            {
                StudentAge = 12,
                StudentName = "梁非凡",
                School = school,
                StudentId = 0
            };
            myDbContext.Student.Add(student);
            myDbContext.SaveChanges();
        }
    }


   public class Province
    {
        public int ProvinceId { get; set; }
        public string ProvinceName { get; set; }

        public string ProvinceLeaderName { get; set; }
    }


public class School
    {
        public int SchoolId { get; set; }
        public int StudentId { get; set; }

        public string SchAddress { get; set; }

        public string SchName { get; set; }

        public Province Province { get; set; }
    }


public class MyDbContext : DbContext
    {
        /// <summary>
        /// 配置数据连接信息
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer("Server=.;DataBase=TestMyDb;Uid=sa;Pwd=123456");
            base.OnConfiguring(optionsBuilder);
        }


        public DbSet<Student> Student { get; set; }

        public DbSet<School> School { get; set; }
    }

public class Student
    {
        public int StudentId { get; set; }

        public string StudentName { get; set; }

        public int StudentAge { get; set; }


        //导航属性
        public School School { get; set; }
    }

 

  

上一篇:Distributing Custom Apps


下一篇:SQL:HAVING 和 WHERE