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); }
static void Main() { try { var dbContext = new DataContext(); //dbContext.Database.ExecuteSqlCommand("delete from category"); dbContext.Database.ExecuteSqlCommand("delete from product"); dbContext.Database.ExecuteSqlCommand("delete from [order]"); var order = new Order {Id = 1, Buyer = "Foobar"}; order.Products = new List<Product>(); var product = new Product {Id = 2, Name = "Foobar"}; order.Products.Add(product); dbContext.Orders.Add(order); //dbContext.Products.Add(); //dbContext.Categories.Add(new Category() {Id = 3, Name = "Foobar"}); dbContext.SaveChanges(); } catch (Exception e) { Console.WriteLine(e); } }
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 Seed(DataContext context) { var user = new User() { Id = 1, Email = "*****@*****.**", Username = "******", FirstName = "Rod", LastName = "Johnson", LastLogin = DateTime.UtcNow, Gender = Gender.Male, Address = "Admin address", PhoneNumber = "555-555-5555", IsLoginAllowed = true, IsAccountClosed = false, IsAccountVerified = true, Created = DateTime.UtcNow, Tenant = "default", // password is "admin" HashedPassword = "******", PasswordChanged = DateTime.UtcNow, FailedLoginCount = 0, Updated = DateTime.UtcNow }; user.Claims.Add(new UserClaim() { Type = ClaimTypes.Role, Value = "Admin" }); user.Claims.Add(new UserClaim() { Type = ClaimTypes.Role, Value = "Super Admin" }); context.Users.AddOrUpdate(user); context.SaveChanges(); }
private void UnpublishCurrentVersion(DataContext context) { var pageInstance = context.PageInstances.SingleOrDefault(x => x.PageId == PageId && x.LanguageId == LanguageId && x.Status == PageInstanceStatus.Published); if (pageInstance == null) { return; } pageInstance.Status = PageInstanceStatus.Archived; context.SaveChanges(); Data.PropertyData.RemovePropertiesFromCache(PageId, LanguageId, pageInstance.CurrentVersion); if (pageInstance.PageUrl != UrlSegment) { var currentPage = PageFactory.GetPage(PageId, LanguageId); RedirectManager.StorePageLinks(currentPage); } }
public void Publish(bool keepAsWorkingCopy = false) { using (var context = new DataContext()) { var pageInstance = context.PageInstances.Single(x => x.PageInstanceId == PageInstanceId); if (keepAsWorkingCopy && pageInstance.CurrentVersion != 1) { throw new Exception("Only the very first working copy need to be stored without being published using the keepAsWorkingCopy flag"); } if (!keepAsWorkingCopy) { if (pageInstance.StartPublish == null) { pageInstance.StartPublish = DateTime.Now.ToUniversalTime(); } UnpublishCurrentVersion(context); pageInstance.Status = PageInstanceStatus.Published; context.SaveChanges(); } PageFactory.UpdatePageIndex(pageInstance, ParentId, RootId, TreeLevel, PageTypeId, SortIndex); CacheManager.RemoveRelated(ParentId); CacheManager.RemoveRelated(PageId); if (!keepAsWorkingCopy) { PageFactory.RaisePagePublished(PageId, LanguageId, CurrentVersion); } } }
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); }