I am new to vs2012.I am trying to run MVC3 tutorial to experience the working of vs2012. I have ran thesame tutorial on vs2010 successfully and inserted new record. Now I want to run thesame tutorial on vs2012. On both cases, i am using sqlserver management studio and entity framework(dotnet 4.0).I used SSMS2008 for VS2010 and SSMS2012 for VS2012. Program ran successfully on vs2012 but was unable to insert new record to database.Note i have googled this and tried
many suggestions all to no avail.. Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LatestBlog.Models;
using System.Data.EntityModel;
using System.Text;
namespace LatestBlog.Controllers
{
public class PostsController : Controller
{
private BlogEntities model = new BlogEntities();
public ActionResult Index()
{
return View();
}
[ValidateInput(false)]
public ActionResult Update(int? id, string title, string body, DateTime datetime, string tags)
{
if (!IsAdmin)
{
return RedirectToAction("Index");
}
Post post = GetPost(id);
post.Title = title;
post.Body = body;
post.DateTime = datetime;
post.Tags.Clear();
tags = tags ?? string.Empty;
string[] tagNames = tags.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string tagname in tagNames)
{
post.Tags.Add(GetTag(tagname));
}
if (!id.HasValue)
{
try
{
//model.AddToPost(post);
model.Posts.Add(post);
}
catch (Exception exp)
{
throw new Exception("ERROR: Unable to Add post to database - " + exp.Message.ToString(), exp);
}
}
model.SaveChanges();
return RedirectToAction("Details", new { id = post.ID });
}
public ActionResult Edit(int? id)
{
Post post = GetPost(id);
StringBuilder tagList = new StringBuilder();//...........................
foreach (Tag tag in post.Tags)
{
tagList.AppendFormat("{0}", tag.Name);
}
ViewBag.Tags = tagList.ToString();
return View(post);
}
private Tag GetTag(string tagName)
{
return model.Tags.Where(x => x.Name == tagName).FirstOrDefault() ?? new Tag() { Name = tagName };
}
private Post GetPost(int? id)
{
return id.HasValue ? model.Posts.Where(x => x.ID == id).First() : new Post() { ID = -1 };
}
public bool IsAdmin
{
get
{
// To do, don't just return true
return true; //Session["IsAdmin"] != null && (bool)Session["IsAdmin"];
}
}
}
}
Connection Strings (I have tried):
<add name="BlogEntities" connectionString="metadata=res://*/Models.Blog.csdl|res://*/Models.Blog.ssdl|res://*/Models.Blog.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Blog;integrated security=True;MultipleActiveResultSets=True; " providerName="System.Data.EntityClient" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-LatestBlog-20150104203236;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-LatestBlog-20150104203236.mdf" />