【问题标题】:Unable to add data into database MVC 5 code first approach无法将数据添加到数据库 MVC 5 代码优先方法
【发布时间】:2015-10-21 12:21:05
【问题描述】:

我正在使用实体框架 6 和代码优先方法。我有 3 个模型类 用户、国家和城市。我正在尝试将用户添加到数据库但无法执行此操作。 这是我的用户类。

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

    }

控制器方法

    [HttpPost]
    public ActionResult Register(User user)
    {

        User reg = new User() { 
            cityId = 2,
            firstName = "U",
            lastName = "v",
            email = "u33@gmail.com",
            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);
    }

进入catch语句,不添加到数据库中。

捕捉错误

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 抓到了什么?
  • 异常:抛出:“更新条目时发生错误。有关详细信息,请参阅内部异常。” (System.Data.Entity.Core.UpdateException) 引发了 System.Data.Entity.Core.UpdateException:“更新条目时发生错误。有关详细信息,请参阅内部异常。”时间:10/21/2015 5:25:41 PM 线程:工作线程[5576]
  • 内部异常怎么说..?
  • 在哪里可以找到内部异常?

标签: c# asp.net entity-framework-6


【解决方案1】:

由于DateTime 是一种值类型,当您不想设置它时需要使用Nullable<DateTime>(或DateTime?),因为DateTime.MinValue(DateTime 的默认值)不在范围内许多 Sql DB DateTime 字段的可接受值。

修复:

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

第二种解决方案是在创建Person 时为joinDate 分配一个值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2015-08-20
    • 1970-01-01
    相关资源
    最近更新 更多