private async Task BuildPath()
        {
            List <PlaceGroupDAO> PlaceGroupDAOs = await DataContext.PlaceGroup
                                                  .Where(x => x.DeletedAt == null)
                                                  .AsNoTracking().ToListAsync();

            Queue <PlaceGroupDAO> queue = new Queue <PlaceGroupDAO>();

            PlaceGroupDAOs.ForEach(x =>
            {
                if (!x.ParentId.HasValue)
                {
                    x.Path  = x.Id + ".";
                    x.Level = 1;
                    queue.Enqueue(x);
                }
            });
            while (queue.Count > 0)
            {
                PlaceGroupDAO Parent = queue.Dequeue();
                foreach (PlaceGroupDAO PlaceGroupDAO in PlaceGroupDAOs)
                {
                    if (PlaceGroupDAO.ParentId == Parent.Id)
                    {
                        PlaceGroupDAO.Path  = Parent.Path + PlaceGroupDAO.Id + ".";
                        PlaceGroupDAO.Level = Parent.Level + 1;
                        queue.Enqueue(PlaceGroupDAO);
                    }
                }
            }
            await DataContext.BulkMergeAsync(PlaceGroupDAOs);
        }
        public async Task <bool> Delete(PlaceGroup PlaceGroup)
        {
            PlaceGroupDAO PlaceGroupDAO = await DataContext.PlaceGroup.Where(x => x.Id == PlaceGroup.Id).FirstOrDefaultAsync();

            await DataContext.PlaceGroup.Where(x => x.Path.StartsWith(PlaceGroupDAO.Id + ".")).UpdateFromQueryAsync(x => new PlaceGroupDAO {
                DeletedAt = StaticParams.DateTimeNow, UpdatedAt = StaticParams.DateTimeNow
            });

            await DataContext.PlaceGroup.Where(x => x.Id == PlaceGroup.Id).UpdateFromQueryAsync(x => new PlaceGroupDAO {
                DeletedAt = StaticParams.DateTimeNow, UpdatedAt = StaticParams.DateTimeNow
            });

            await BuildPath();

            return(true);
        }
        public async Task <bool> BulkMerge(List <PlaceGroup> PlaceGroups)
        {
            List <PlaceGroupDAO> PlaceGroupDAOs = new List <PlaceGroupDAO>();

            foreach (PlaceGroup PlaceGroup in PlaceGroups)
            {
                PlaceGroupDAO PlaceGroupDAO = new PlaceGroupDAO();
                PlaceGroupDAO.Id        = PlaceGroup.Id;
                PlaceGroupDAO.ParentId  = PlaceGroup.ParentId;
                PlaceGroupDAO.Name      = PlaceGroup.Name;
                PlaceGroupDAO.Code      = PlaceGroup.Code;
                PlaceGroupDAO.CreatedAt = StaticParams.DateTimeNow;
                PlaceGroupDAO.UpdatedAt = StaticParams.DateTimeNow;
                PlaceGroupDAOs.Add(PlaceGroupDAO);
            }
            await DataContext.BulkMergeAsync(PlaceGroupDAOs);

            await BuildPath();

            return(true);
        }
        public async Task <bool> Create(PlaceGroup PlaceGroup)
        {
            PlaceGroupDAO PlaceGroupDAO = new PlaceGroupDAO();

            PlaceGroupDAO.Id        = PlaceGroup.Id;
            PlaceGroupDAO.ParentId  = PlaceGroup.ParentId;
            PlaceGroupDAO.Name      = PlaceGroup.Name;
            PlaceGroupDAO.Code      = PlaceGroup.Code;
            PlaceGroupDAO.Path      = "";
            PlaceGroupDAO.Level     = 1;
            PlaceGroupDAO.CreatedAt = StaticParams.DateTimeNow;
            PlaceGroupDAO.UpdatedAt = StaticParams.DateTimeNow;
            DataContext.PlaceGroup.Add(PlaceGroupDAO);
            await DataContext.SaveChangesAsync();

            PlaceGroup.Id = PlaceGroupDAO.Id;
            await SaveReference(PlaceGroup);
            await BuildPath();

            return(true);
        }
        public async Task <bool> Update(PlaceGroup PlaceGroup)
        {
            PlaceGroupDAO PlaceGroupDAO = DataContext.PlaceGroup.Where(x => x.Id == PlaceGroup.Id).FirstOrDefault();

            if (PlaceGroupDAO == null)
            {
                return(false);
            }
            PlaceGroupDAO.Id        = PlaceGroup.Id;
            PlaceGroupDAO.ParentId  = PlaceGroup.ParentId;
            PlaceGroupDAO.Name      = PlaceGroup.Name;
            PlaceGroupDAO.Code      = PlaceGroup.Code;
            PlaceGroupDAO.Path      = "";
            PlaceGroupDAO.Level     = 1;
            PlaceGroupDAO.UpdatedAt = StaticParams.DateTimeNow;
            await DataContext.SaveChangesAsync();

            await SaveReference(PlaceGroup);
            await BuildPath();

            return(true);
        }