0

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]

enter image description here

10
  • What's the catched Exception? Commented Oct 21, 2015 at 12:22
  • 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] Commented Oct 21, 2015 at 12:27
  • 2
    And what does say the inner Exception..? Commented Oct 21, 2015 at 12:28
  • Where can I find inner exception ? Commented Oct 21, 2015 at 12:33
  • 1
    stackoverflow.com/questions/6730859/… Commented Oct 21, 2015 at 14:24

1 Answer 1

1

Since DateTime is a value type you need to use a Nullable<DateTime> (or DateTime?) when you do not want to set it because the DateTime.MinValue (default value of a DateTime) is not in the range of acceptable values of many Sql DB DateTime field.

Fix :

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; }
    }
}

Th second solution is to assign a value to the joinDate when you create the Person

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.