public ActionResult Edit(int id)
 {
     using (var dbContext = new BlogDbContext()){
         var model = dbContext.BlogEntries.Single(be => be.Id == id).MapFrom<BlogEntry, BlogEntryModel>();
         return View(model);
     }
 }
 //
 // GET: /Blog/
 public ActionResult Index()
 {
     using (var dbContext = new BlogDbContext()){
         var model = dbContext.Blogs.MapFrom<Blog, BlogModel>();
         return View(model);
     }
 }
 public ActionResult Edit(int id)
 {
     using (var dbContext = new BlogDbContext())
     {
         var model = dbContext.Blogs.Where(b => b.Id == id).Single().MapFrom<Blog, BlogModel>();
         return View(model);
     }
 }
 public ActionResult Index()
 {
     ViewBag.Message = "Welcome to ASP.NET MVC!";
     using (var dbContext = new BlogDbContext()){
         var model = dbContext.Blogs.First().MapFrom<Blog, BlogModel>();
         return View(model);
     }
 }
 public bool ValidateUser(string userName, string password)
 {
     using (var dbContext = new BlogDbContext()){
         var user = dbContext.Users.SingleOrDefault(u => u.Name == userName);
         if (user == null)
             return false;
         return user.PasswordHash == GetHash(password);
     }
 }
 public ActionResult Create(BlogModel model)
 {
     if (ModelState.IsValid){
         using (var dbContext= new BlogDbContext()){
             var entity = model.MapFrom<BlogModel, Blog>();
             dbContext.Blogs.Add(entity);
             dbContext.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     return View(model);
 }
 public ActionResult Edit(int id, BlogEntryModel model)
 {
     if (ModelState.IsValid){
         using (var dbContext = new BlogDbContext()){
             var blogEntry = dbContext.BlogEntries.Single(be => be.Id == id);
             model.MapTo(blogEntry);
             dbContext.SaveChanges();
         }
         return RedirectToShow(model);
     }
     return View(model);
 }
 public MembershipCreateStatus CreateUser(string userName, string password, string email)
 {
     using (var dbContext = new BlogDbContext()){
         var user = dbContext.Users.SingleOrDefault(u => u.Name == userName || u.EMail == email);
         if (user != null)
             return MembershipCreateStatus.DuplicateUserName;
         user = new WebUser {Name = userName, EMail = email, PasswordHash = GetHash(password)};
         dbContext.Users.Add(user);
         dbContext.SaveChanges();
         return MembershipCreateStatus.Success;
     }
 }
 public ActionResult Edit(int id, BlogModel model)
 {
     if (ModelState.IsValid)
     {
         using (var dbContext = new BlogDbContext()){
             var entity = dbContext.Blogs.Single(bm => bm.Id == id);
             model.MapTo(entity);
             dbContext.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     return View(model);
 }
 public ActionResult AddComment(int id, CommentModel model)
 {
     using (var dbContext = new BlogDbContext()){
         var blogEntry = dbContext.BlogEntries.Single(be => be.Id == id);
         if (ModelState.IsValid){
             var newComment = model.MapFrom<CommentModel, Comment>();
             blogEntry.Comments.Add(newComment);
             dbContext.SaveChanges();
             return RedirectToShow(blogEntry.MapFrom<BlogEntry, BlogEntryModel>());
         }
         return View("Show", blogEntry.MapFrom<BlogEntry, BlogEntryModel>());
     }
 }
 public bool ChangePassword(string userName, string oldPassword, string newPassword)
 {
     using (var dbContext = new BlogDbContext()){
         var user = dbContext.Users.SingleOrDefault(u => u.Name == userName);
         if (user == null)
             return false;
         if (user.PasswordHash == GetHash(oldPassword)){
             user.PasswordHash = GetHash(newPassword);
             dbContext.SaveChanges();
             return true;
         }
     }
     return false;
 }
 public ActionResult Create(int id, BlogEntryModel model)
 {
     if (ModelState.IsValid){
         using (var dbContext = new BlogDbContext()){
             var blog = dbContext.Blogs.Single(b => b.Id == id);
             var blogEntry = new BlogEntry();
             blogEntry.Author = GetCurrentWebUser(dbContext);
             model.MapTo(blogEntry);
             blog.BlogEntries.Add(blogEntry);
             dbContext.SaveChanges();
         }
         return RedirectToAction("Details", "Blog", new {id = id});
     }
     return View(model);
 }
 private WebUser GetCurrentWebUser(BlogDbContext dbContext)
 {
     return dbContext.Users.Single(u => u.Name == User.Identity.Name);
 }
 //
 // GET: /BlogEntry/
 public ActionResult Show(string date, string title)
 {
     DateTime blogDate = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
     using (var dbContext = new BlogDbContext()){
         var query = from be in dbContext.BlogEntries
                     where be.Title == title &&
                           be.Date == blogDate
                     select be;
         var model = query.Single().MapFrom<BlogEntry, BlogEntryModel>();
         return View(model);
     }
 }
 public ActionResult About()
 {
     var dbContext = new BlogDbContext();
     return View();
 }