I wrote the following code for inserting data into database using EDM
but I got the EROR as
The INSERT statement conflicted with the FOREIGN KEY constraint \"Comments_Blog\". The conflict occurred in database............
Below is my code
public class BlogDbContext:DbContext
{
public DbSet<Blog> Blog { get; set; }
public DbSet<Comments> Comments { get; set; }
}
public class Comments
{
[Key]
public int CommentId { get; set; }
public string Comment { get; set; }
//[ForeignKey]
public int BlogId { get; set; }
[Required(ErrorMessage = "BlogName is required")]
public string BlogName { get; set; }//If I didnt add this I cant get get this in view only BlogId and Comment only cmae
public virtual Blog Blog { get; set; }
}
public class Blog
{
[Key]
public int BlogId { get; set; }
[Required(ErrorMessage = "BlogName is required")]
public string BlogName { get; set; }
[Required(ErrorMessage = "Description is required")]
[StringLength(120, ErrorMessage = "Description Name Not exceed more than 120 words")]
public string Description { get; set; }
public string Body { get; set; }
public virtual List<Comments> Comments_List { get; set; }
}
this is my controller
public class Default1Controller : Controller
{
BlogDbContext _db = new BlogDbContext();
//
// GET: /Default1/
public ActionResult Index()
{
var GenreLst = new List<string>();
var GenreQry = from d in _db.Blog
orderby d.BlogName
select d.BlogName;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
return View(_db.Comments.ToList());
}
//
// GET: /Default1/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Default1/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Default1/Create
[HttpPost]
public ActionResult Create(Comments Comments)
{
if (ModelState.IsValid)
{
_db.Comments.Add(Comments);
_db.SaveChanges();
return RedirectToAction("Index");
}
chandana G
Member
7 Points
69 Posts
How To overcome this error in mvc insert statement
Apr 26, 2012 11:55 AM|LINK
Hi ,
I wrote the following code for inserting data into database using EDM
but I got the EROR as
The INSERT statement conflicted with the FOREIGN KEY constraint \"Comments_Blog\". The conflict occurred in database............
Below is my code
public class BlogDbContext:DbContext
{
public DbSet<Blog> Blog { get; set; }
public DbSet<Comments> Comments { get; set; }
}
public class Comments
{
[Key]
public int CommentId { get; set; }
public string Comment { get; set; }
//[ForeignKey]
public int BlogId { get; set; }
[Required(ErrorMessage = "BlogName is required")]
public string BlogName { get; set; }//If I didnt add this I cant get get this in view only BlogId and Comment only cmae
public virtual Blog Blog { get; set; }
}
public class Blog
{
[Key]
public int BlogId { get; set; }
[Required(ErrorMessage = "BlogName is required")]
public string BlogName { get; set; }
[Required(ErrorMessage = "Description is required")]
[StringLength(120, ErrorMessage = "Description Name Not exceed more than 120 words")]
public string Description { get; set; }
public string Body { get; set; }
public virtual List<Comments> Comments_List { get; set; }
}
this is my controller
public class Default1Controller : Controller
{
BlogDbContext _db = new BlogDbContext();
//
// GET: /Default1/
public ActionResult Index()
{
var GenreLst = new List<string>();
var GenreQry = from d in _db.Blog
orderby d.BlogName
select d.BlogName;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
return View(_db.Comments.ToList());
}
//
// GET: /Default1/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /Default1/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Default1/Create
[HttpPost]
public ActionResult Create(Comments Comments)
{
if (ModelState.IsValid)
{
_db.Comments.Add(Comments);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(Comments);
}
If anybody knows the Answer Plz help me
ignatandrei
All-Star
137549 Points
22117 Posts
Moderator
MVP
Re: How To overcome this error in mvc insert statement
Apr 26, 2012 12:23 PM|LINK
add a blogid to the comment ( put as hidden input)
chandana G
Member
7 Points
69 Posts
Re: How To overcome this error in mvc insert statement
Apr 27, 2012 04:02 AM|LINK
I declared BlogId in comment also
how to put it as hidden input
ignatandrei
All-Star
137549 Points
22117 Posts
Moderator
MVP
Re: How To overcome this error in mvc insert statement
Apr 27, 2012 06:27 AM|LINK
show code
chandana G
Member
7 Points
69 Posts
Re: How To overcome this error in mvc insert statement
Apr 28, 2012 06:47 AM|LINK
I put it as hidden field but again same error came
<%: Html.HiddenFor(modelItem => item.BlogId) %>
Young Yang -...
All-Star
21742 Points
1825 Posts
Microsoft
Re: How To overcome this error in mvc insert statement
May 02, 2012 10:10 AM|LINK
Hi
Why are you adding BlogName filed in Comments Model? I think it is not necessary, please remove it.
Regards
Young Yang
Feedback to us
Develop and promote your apps in Windows Store
ignatandrei
All-Star
137549 Points
22117 Posts
Moderator
MVP
Re: How To overcome this error in mvc insert statement
May 02, 2012 10:49 AM|LINK
What is the value of blogid?
chandana G
Member
7 Points
69 Posts
Re: How To overcome this error in mvc insert statement
May 02, 2012 11:24 AM|LINK
I Didnt give the relation between two tables
now I reolve the error but again I facing one problem
I have two tales(code first basis)
user and people
I want to get data from user for this I wrote the following code
public class Users
{
[Key]
public int UserId { get; set; }
[Required]
[StringLength(100)]
public string LastName { get; set; }
[Required]
[StringLength(100)]
public string Name { get; set; }
[ScriptIgnore]
public virtual ICollection<people> people { get; set; }
}
public class people
{
[Key]
public int groupid { get; set; }
[Required]
public string name { get; set; }
[Required]
public int UserId { get; set; }
[ScriptIgnore]
public virtual Users user { get; set; }
}
I only get the lastname in dropdown list
is it possible to get the Name(third column of parent) also in dropdownlist
asp.netmvc3