public ActionResult Create(Community community) { if (ModelState.IsValid) { community.Id = Guid.NewGuid(); db.Communities.AddObject(community); db.SaveChanges(); var templateSceneIdString = this.Request.Form["templateSceneId"]; if (!string.IsNullOrEmpty(templateSceneIdString)) { // NOTE: make sure community ID is already created when move to "code first" using (var cms = new CmsEntities()) { var scene = cms.Scenes.Attach(community.GetScene()); var templateSceneId = long.Parse(templateSceneIdString); var templateScene = cms.Scenes.First(s => s.Id == templateSceneId); scene.ApplyTemplate(cms, templateScene); cms.SaveChanges(); } } return RedirectToAction("Index"); } ViewBag.ZoneId = new SelectList(db.Zones, "Id", "Name", community.ZoneId); return View(community); }
/// <summary> /// Get community's scene. If it doesn't exists it creates one. /// </summary> /// <param name="community">Community to get scene for</param> /// <returns></returns> public static Scene GetScene(this Community community) { using (var db = new CmsEntities()) { var scene = db.Scenes.FirstOrDefault(s => s.OwnerGuidId == community.Id); if (scene == null) { scene = db.Scenes.Add(new Scene { OwnerGuidId = community.Id }); db.SaveChanges(); } // trick to ensure that walls, bricks and shared bricks are loaded in current DB context for given scene var bricks = scene.Walls.SelectMany(w => w.Bricks).ToList().OfType<LinkableBrick>().Select(b => b.LinkedBrick).ToList(); return scene; } }