导航属性 -级联-延迟加载-贪婪加载

 public partial class Company
    {
        //private ILazyLoader _lazyLoader = null;
        //public Company(ILazyLoader lazyLoader)
        //{
        //    this._lazyLoader = lazyLoader;
        //}
        //这上面是延迟加载
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }
        public DateTime? CreateTime { get; set; }
        public int CreatorId { get; set; }
        public int? LastModifierId { get; set; }
        public DateTime? LastModifyTime { get; set; }
        public string Description { get; set; }
        /// <summary>
        /// 导航属性
        /// </summary>public virtual List<SysUser> SysUser { get; set; }   //要声明为虚   sysUser 为子表  

    }
public partial class SysUser
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public int Status { get; set; }
        public string Phone { get; set; }
        public string Mobile { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public long? Qq { get; set; }
        public string WeChat { get; set; }
        public int? Sex { get; set; }
        public DateTime? LastLoginTime { get; set; }
        public DateTime? CreateTime { get; set; }
        public int? CreateId { get; set; }
        public DateTime? LastModifyTime { get; set; }
        public int? LastModifyId { get; set; }
        public int? CompanyId { get; set; }
        /// <summary>
        /// 应用属性
        /// </summary>
        public virtual Company Company { get; set; }
    }
   {
                        //关于新增
                        using (CustomerDbContext context = new CustomerDbContext())
                        {
                            Company company = new Company()
                            {
                                SysUser = new List<SysUser>()
                                {
                                    new SysUser(){
                                         LastLoginTime=DateTime.Now,
                                         LastModifyTime=DateTime.Now,
                                         CreateId=1,
                                         CreateTime=DateTime.Now,
                                         Email="",
                                         Mobile="18672713698"
                                    }
                                },
                                CompanyName = "阿里的武汉公司",
                                LastModifierId = 1,
                                CreateTime = DateTime.Now,
                                CreatorId = 1,
                                LastModifyTime = DateTime.Now,
                            };
                            context.Companies.Add(company);
                            context.SaveChanges();
                        }
                        //新增的时候会先添加母表再添加子表 ,而且子表中的companyID 会自动写入, 这也就是级联的新增
                        //查询:EFCore中,默认情况下,查询主表,子表默认为null List<SysUser>--默认不加载;
                        //1.想要加载: 
                            //贪婪加载:Include--在查询主表--自动的把字表也查询出来
                        //2.延迟加载:就是在我们需要的时候,才去查询数据 有两种方式来完成
                        //a.通过代理
                        //b.ILazyLoader    在实体中构造函数标出
                        using (CustomerDbContext context = new CustomerDbContext())
                        {
                            //延迟加载:    默认是延迟加载来提高性能的
                            List<Company> companiList = context.Companies.ToList();
                           // 贪婪加载     子表的数据会一起加载出来,不写include的话,只有在使用子表的数据时候才去加载
                            List<Company> companiList1 = context.Companies.Include(c => c.SysUser).ToList();
                            //方法一:    //使用带来支持延迟加载 需要引入一个程序集:Microsoft.EntityFrameworkCore.Proxies 
                            //方法二:      ILazyLoader
                            //支持了导航属性的延迟加载后,只有在使用到数据的时候才会正式的去查询数据库--
                            companiList = context.Companies.ToList();
                            Console.WriteLine("**************************************");
                            foreach (Company item in companiList)
                            {
                                Console.WriteLine(item.CompanyName);

                                List<SysUser> userList = item.SysUser;
                                foreach (SysUser us in userList)   //只有在使用子表的数据时候才去加载
                                {
                                    Console.WriteLine(us.Name);
                                    Company company1 = us.Company;
                                    Console.WriteLine(company1.CompanyName);
                                    foreach (var us1 in company1.SysUser)
                                    {
                                        Console.WriteLine(us1.Name);
                                    }

                                }

                            }
                        }
                    }
  直接删除子表的数据话  sysUser  无问题,删除母表 如果子表有使用母表的companyId 是不能删的,需要在数据库中设置级联删除,级联更改,外键关系那个地方  

 

上一篇:MySQL出现中文乱码问题,已经解决。


下一篇:DeepFaceLab更新至2019.12.29