public void SaveAndPublish() {
            var languageId = Language.CurrentLanguageId;

            using (var context = new DataContext())
            {
                var siteEntity = context.Sites.SingleOrDefault(x => x.SiteId == SiteId);

                if (siteEntity == null) {
                    siteEntity = new SiteEntity {
                        SiteId = SiteId
                    };

                    context.Add(siteEntity);
                    context.SaveChanges();
                }

                siteEntity.Author = HttpContext.Current.User.Identity.Name;
                siteEntity.ChildSortDirection = ChildSortDirection;
                siteEntity.ChildSortOrder = ChildSortOrder;
                siteEntity.Name = Name;
                siteEntity.UpdateDate = DateTime.Now.ToUniversalTime();

                context.SaveChanges();

                // ---------------

                var propertiesForSite = context.SiteProperties.Where(x => x.SiteId == SiteId && x.LanguageId == languageId).ToList();

                foreach (var propertyItem in Property) {
                    var propertyEntity = propertiesForSite.Find(c => c.PropertyId == propertyItem.PropertyId);

                    if (propertyEntity == null) {
                        propertyEntity = new SitePropertyEntity {
                            LanguageId = languageId,
                            SiteId = SiteId,
                            PropertyId = propertyItem.PropertyId
                        };
                        context.Add(propertyEntity);
                        propertiesForSite.Add(propertyEntity);
                    }

                    propertyEntity.SiteData = GetSerializedPropertyValue(propertyItem);
                }

                context.SaveChanges();
            }

            SiteFactory.UpdateSite(this);
            CacheManager.RemoveRelated(SiteId);


            SiteFactory.RaiseSitePublished(SiteId, languageId);
        }
 private static void AddPageLinksToDatabase(IEnumerable<RedirectEntity> redirects) {
     using (var context = new DataContext()) {
         foreach (var redirect in redirects) {
             if (context.Redirects.Any(r => r.UrlHash == redirect.UrlHash)) {
                 continue;
             }
             
             context.Add(redirect);
         }
         try {
             context.SaveChanges();
         }
         catch (Exception exception) {
             Logger.Write(exception, Logger.Severity.Major);
         }
     }
 }
        public void Save() {
            using (var context = new DataContext()) {
                var pageEntity = context.Pages.SingleOrDefault(x => x.PageId == PageId);

                if (pageEntity == null) {
                    pageEntity = new PageEntity {
                        PageId = PageId,
                        PageTypeId = PageTypeId,
                        ParentId = ParentId,
                        RootId = RootId,
                        SortOrder = SortIndex,
                        TreeLevel = TreeLevel
                    };

                    context.Add(pageEntity);
                    context.SaveChanges();
                }

                // ---------------

                var pageInstance = context.PageInstances.SingleOrDefault(x => x.PageInstanceId == PageInstanceId);

                if (pageInstance == null) {
                    CurrentVersion = context.PageInstances.Where(x => x.PageId == PageId && x.LanguageId == LanguageId).Max(x => x.CurrentVersion) + 1;

                    pageInstance = new PageInstanceEntity {
                        PageId = PageId,
                        LanguageId = LanguageId,
                        CreatedDate = DateTime.Now.ToUniversalTime(),
                        CurrentVersion = CurrentVersion,
                        Status = PageInstanceStatus.WorkingCopy
                    };

                    context.Add(pageInstance);
                }

                pageInstance.Author = HttpContext.Current.User.Identity.Name;
                pageInstance.ChildSortDirection = ChildSortDirection;
                pageInstance.ChildSortOrder = ChildSortOrder;
                pageInstance.PageName = PageName;
                pageInstance.StartPublish = StartPublish;
                pageInstance.StopPublish = StopPublish;
                pageInstance.UpdateDate = DateTime.Now.ToUniversalTime();
                pageInstance.VisibleInMenu = VisibleInMenu;
                pageInstance.VisibleInSitemap = VisibleInSiteMap;
                
                EnsurePageUrl();

                pageInstance.PageUrl = UrlSegment;

                context.SaveChanges();

                PageInstanceId = pageInstance.PageInstanceId;

                // ---------------

                var pagePropertiesForPage = context.PageProperties.Where(x => x.PageId == PageId && x.LanguageId == LanguageId && x.Version == CurrentVersion).ToList();

                foreach (var propertyItem in Property) {
                    var propertyEntity = pagePropertiesForPage.Find(c => c.PropertyId == propertyItem.PropertyId);

                    if (propertyEntity == null) {
                        propertyEntity = new PagePropertyEntity {
                            LanguageId = LanguageId,
                            PageId = PageId,
                            PropertyId = propertyItem.PropertyId,
                            Version = CurrentVersion
                        };
                        context.Add(propertyEntity);
                        pagePropertiesForPage.Add(propertyEntity);
                    }

                    propertyEntity.PageData = GetSerializedPropertyValue(propertyItem);
                }

                context.SaveChanges();
            }

            // Allow property types to execute code when a property of that type is saved
            foreach (var propertyItem in Property) {
                if (propertyItem == null) {
                    continue;
                }

                var propertyData = propertyItem.PropertyData as IPageSavedHandler;
                if (propertyData != null) {
                    propertyData.PageSaved(this);
                }
            }

            if (CurrentVersion == 1) {
                Publish(true);
            }

            PageFactory.RaisePageSaved(PageId, LanguageId, CurrentVersion);
        }