I am using entity framework 6 with code first approach. I have 3 model classes User,country and city. I am trying to add user to database but unable to do it. Here is my user class.
public class User
{
public int userId { get; set; }
public int cityId { get; set; }
public String firstName { get; set; }
public String lastName { get; set; }
public String gender { get; set; }
public String email { get; set; }
public String password { get; set; }
public String photo { get; set; }
public DateTime joinDate { get; set; }
//public City city { get; set; }
//public Country country { get; set; }
public virtual City city { get; set; }
private String FullName
{
get { return firstName + lastName; }
}
}
Controller method
[HttpPost]
public ActionResult Register(User user)
{
User reg = new User() {
cityId = 2,
firstName = "U",
lastName = "v",
email = "[email protected]",
password = "123",
gender = "Male",
photo = "asd",
};
try
{
db.Users.Add(reg);
db.SaveChanges();
// TODO: Add insert logic here
return View("Index","Home");
}
catch
{
return RedirectToAction("Index", "Home");
// return View("Register", user);
}
// return View("Register", user);
}
it goes to catch statement and does not add into database.
Catch Error
Exception:Thrown: "An error occurred while updating the entries. See the inner exception for details." (System.Data.Entity.Core.UpdateException)
A System.Data.Entity.Core.UpdateException was thrown: "An error occurred while updating the entries. See the inner exception for details."
Time: 10/21/2015 5:25:41 PM
Thread:Worker Thread[5576]

Exception?