Here is my ActionMethod, it does not populate data to database.
private StudentDBContext db = new StudentDBContext();
public ActionResult PopulateData()
{
Student objStu = new Student();
for(int i=0;i<2;i++)
{
objStu.ID = i+1;
objStu.name = "something";
db.Students.Add(objStu);
db.SaveChanges();
}
return View();
}
Only time when it does is when I use it without loop(as shown below) why is that so?
public ActionResult PopulateData()
{
Student objStu = new Student();
//for(int i=0;i<2;i++)
//{
objStu.ID = 1;
objStu.name = "something";
db.Students.Add(objStu);
db.SaveChanges();
//}
return View();
}
Student objStu = new Student();inside the loop.