示例#1
0
        public async Task AddSiteCollectionsAsync(string siteId, IList <string> siteCollectionIds)
        {
            var existingIds = await _db.SiteCollectionMaps.AsNoTracking()
                              .Where(x => x.SiteId == siteId)
                              .Where(x => siteCollectionIds.Contains(x.SiteCollectionId))
                              .Select(x => x.SiteCollectionId)
                              .ToListAsync();

            foreach (var siteCollectionId in siteCollectionIds)
            {
                if (existingIds.Contains(siteCollectionId) == false)
                {
                    var newItem = new SiteCollectionMap();
                    newItem.Site = await _db.Sites.Where(x => x.Id == siteId).FirstOrDefaultAsync();

                    newItem.SiteCollection = await _db.SiteCollections.Where(x => x.Id == siteCollectionId).FirstOrDefaultAsync();

                    if ((newItem.Site != null) && (newItem.SiteCollection != null))
                    {
                        _db.SiteCollectionMaps.Add(newItem);
                    }
                }
            }
            await _db.SaveChangesAsync();
        }
        public async Task <bool> SitesAddNewAsync(string collectionId, string siteId)
        {
            Ensure.Argument.NotNull(collectionId);
            Ensure.Argument.NotNull(siteId);

            try
            {
                if ((await _db.SiteCollectionMaps.AnyAsync(x => x.SiteCollectionId == collectionId && x.SiteId == siteId)) == false)
                {
                    var newSite = new SiteCollectionMap();
                    newSite.Site = await _db.Sites.FirstOrDefaultAsync(x => x.Id == siteId);

                    newSite.SiteCollection = await _db.SiteCollections.FirstOrDefaultAsync(x => x.Id == collectionId);

                    _db.SiteCollectionMaps.Add(newSite);
                    await _db.SaveChangesAsync();
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }