EF中数据导航的使用(一对一,一对多,多对多)

EF中数据导航的一对一,一对多配置和使用

 

配置:

    public class LingbugDbContext : DbContext
    {
        public LingbugDbContext() : base("name=Default")
        {

        }

        public DbSet Order { get; set; }

        public DbSet OrderDetail { get; set; }

        public DbSet DeliveryOrder { get; set; }

        public DbSet DeliveryOrderDetail { get; set; }

        public DbSet StudentInfo { get; set; }

        public DbSet TeacherInfo { get; set; }

        public DbSet RelationShipInfo { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //订单
            //每个订单有多个订单明细(一对多)
            modelBuilder.Entity().HasMany(r => r.OrderDetailList).WithRequired(r => r.Order);

            //订单明细
            //每个订单明细对应一个发货单(一对一)
            modelBuilder.Entity().HasRequired(r => r.Order).WithMany().HasForeignKey(r => r.SupplierOrderId);

            //发货单
            //每个发货单对应一个订单(一对一)
            modelBuilder.Entity().HasRequired(r => r.Order).WithMany().HasForeignKey(r => r.SupplierOrderId);
            //每个发货单对应多个发货单明细(一对多)
            modelBuilder.Entity().HasMany(r => r.DeliveryOrderDetailList).WithRequired(r => r.DeliveryOrder);

            //发货单明细
            //每个发货单明细对应一个发货单(一对一)
            modelBuilder.Entity().HasRequired(r => r.DeliveryOrder).WithMany().HasForeignKey(r => r.SupplierDeliveryOrderId);
            //每个发货单明细对应一个订单明细(一对一)
            modelBuilder.Entity().HasRequired(r => r.OrderDetail).WithMany().HasForeignKey(r => r.SupplierOrderDetailsId);

            //modelBuilder.Entity().HasMany(r => r.StudentList).WithMany(r => r.TeacherList).Map(r =>
            //    {
            //        r.ToTable("RelationShipInfo");
            //        r.MapLeftKey("TeacherId");
            //        r.MapRightKey("StudentId");
            //    });

            //一个关系对应一个学生
            modelBuilder.Entity().HasRequired(r => r.Student).WithMany().HasForeignKey(r => r.StudentId);
            //一个关系对应一个老师:HasRequired标识外键列不可为空,HasOptional标识外键列可以为空
            modelBuilder.Entity().HasOptional(r => r.Teacher).WithMany().HasForeignKey(r => r.TeacherId);

            //一个学生对应多个关系
            modelBuilder.Entity().HasMany(r => r.RelationShipList).WithRequired(r => r.Student);
            //一个老师对应多个关系
            modelBuilder.Entity().HasMany(r => r.RelationShipList).WithRequired(r => r.Teacher);

            base.OnModelCreating(modelBuilder);
        }
    }

 

使用(使用Include会直接join查出来,不用Include,会在后续使用到导航数据时进行懒加载/延迟加载):

        public JsonResult TestEf()
        {
            var logs = new List();
            try
            {
                using (var dbContext = new LingbugDbContext())
                {
                    dbContext.Database.Log = r => logs.Add(r);

                    //查看订单,带出来订单明细(一对多)
                    var orderList = dbContext.Order.Include(r => r.OrderDetailList).ToList();

                    //查询订单明细,带出来订单信息(一对一)
                    var orderDetailList = dbContext.OrderDetail.Include(r => r.Order).ToList();

                    //查询发货单,带出来订单和发货单明细(一对一 + 一对多)
                    var deliveryOrderList = dbContext.DeliveryOrder.Include(r => r.Order).Include(r => r.DeliveryOrderDetailList).ToList();

                    //InitTable(dbContext);

                    //查询学生,带出来中间表,中间表再带出来老师(多对多)
                    var studentListNoLazy = dbContext.StudentInfo.Include(r => r.RelationShipList).Include(r => r.RelationShipList.Select(x => x.Teacher)).ToList();

                    //直接查询,只会查询学生,导航数据不会出来
                    var studentList = dbContext.StudentInfo.ToList();

                    studentList.ForEach(s =>
                    {
                        //这里使用到中间表,会进行查询(懒加载)
                        var studentRelationShipList = s.RelationShipList.ToArray();
                        logs.Add($"学生{s.StudentName}有{studentRelationShipList.Length}个老师");
                        for (int i = 0; i < studentRelationShipList.Length; i++)
                        {
                            //这里使用到老师,也会进行查询(懒加载)
                            logs.Add($"{i + 1}.{studentRelationShipList[i].Teacher.TeacherName}");
                        }
                        logs.Add("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
                    });

                    string test = null;
                }
                logs.Add("Success");
                return Json(true);
            }
            catch (Exception ex)
            {
                logs.Add("Error:" + JsonConvert.SerializeObject(ex));
                return Json(false);
            }
            finally
            {
                System.IO.File.AppendAllLines(@"E:\1.Lingbug\4.虚拟桌面\EfLog.txt", logs);
            }
        }

 

Ending~

你可能感兴趣的:(C#/.NET)