【问题标题】:Cannot save entity one to one EF mvc 5无法一对一保存实体 EF mvc 5
【发布时间】:2017-06-09 22:48:50
【问题描述】:

我正在尝试向数据库中插入一条新记录,没有错误,没有在申请人和申请人通知表中创建新记录。不知道我做错了什么?

申请人

  [Index]
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ApplicantID { get; set; }
    [Required]
    public string ApplicantTitle { get; set; }
    [Required]
    public string Firstname { get; set; }
    [Required]
    public string Lastname { get; set; }
    [Required]
    public string Address { get; set; }
    [Required]
    public string Address1 { get; set; }
    [Required]
    public string Address2 { get; set; }
    [Required]
    public string Address3 { get; set; }
    [Required]
    public string Postcode { get; set; }
    [Required]
    public string CaseReference { get; set; }
    [DataType(DataType.Date)]
    public DateTime DateOfBirth { get; set; }

    /*Spouse*/
    public string SpouseTitle { get; set; }
    public string SpouseFirstname { get; set; }
    public string SpouseLastname { get; set; }
    public string SpouseAddress { get; set; }
    public string SpouseAddress1 { get; set; }
    public string SpouseAddress2 { get; set; }
    public string SpouseAddress3 { get; set; }
    public string SpousePostcode { get; set; }

申请人通知

        [Index]
        [Key, Column("ApplicantID"), ForeignKey("Applicant")]
        public int ApplicantNotificationID { get; set; }
        public bool FirstNotification { get; set; }
        public bool SecondtNotification { get; set; }
        public bool ThirdNotification { get; set; }
        public bool FinalNotification { get; set; }
        public DateTime ReminderDate { get; set; }
        public int ReminderFrequency { get; set; }
        [DataType(DataType.Date)]
        public DateTime? FirstNotificationDate { get; set; }
        [DataType(DataType.Date)]
        public DateTime? SecondNotificationDate { get; set; }
        [DataType(DataType.Date)]
        public DateTime? ThirdNotificationDate { get; set; }
        public bool IsArchive { get; set; }
        public virtual Applicant Applicant { get; set; }

视图模型

        public int ApplicantID { get; set; }
        [Required]
        public string ApplicantTitle { get; set; }
        public string ApplicantFirstname { get; set; }
        public string ApplicantLastname { get; set; }
        public string ApplicantAddress { get; set; }
        public string ApplicantAddress1 { get; set; }
        public string ApplicantAddress2 { get; set; }
        public string ApplicantAddress3 { get; set; }
        public string ApplicantPostcode { get; set; }
        [Required]
        public string ApplicantCaseReference { get; set; }
        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime ApplicantDateOfBirth { get; set; }
        /*Spouse*/
        public string SpouseTitle { get; set; }
        public string SpouseFirstname { get; set; }
        public string SpouseLastname { get; set; }
        public string SpouseAddress { get; set; }
        public string SpouseAddress1 { get; set; }
        public string SpouseAddress2 { get; set; }
        public string SpouseAddress3 { get; set; }
        public string SpousePostcode { get; set; }
        /*Notification*/
        public int ApplicantNotificationID { get; set; }
        public bool FirstNotification { get; set; }
        public bool SecondNotification { get; set; }
        public bool ThirdNotification { get; set; }
        public bool FinalNotification { get; set; }
        public DateTime? ReminderDate { get; set; }

创建方法:

// POST: Applicant/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ApplicantNotificationViewModel model)
{
    var applicant = new Applicant();
    var applicantNotification = new ApplicantNotification();

        if (ModelState.IsValid)
        {
            SetApplicant(model, applicant);
            SetApplicantNotification(model, applicantNotification);

            using (var context = new WorkSmartContext())
            {
                using (var dbContextTransaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        db.Applicants.Add(applicant);
                        context.SaveChanges();
                        db.ApplicantNotifcations.Add(applicantNotification);
                        context.SaveChanges();
                        dbContextTransaction.Commit();
                    }
                    catch (Exception)
                    {
                        dbContextTransaction.Rollback();
                    }
                }

            return RedirectToAction("Index");
        }
    }
    return View(model);
}

【问题讨论】:

  • 为什么要检查model.isValid两次
  • 无相关,但您使用的是视图模型。删除那个糟糕的[Bind] 属性
  • @hasan 抱歉打错了
  • 我的理解是要有 Bind 属性来阻止过度发布?你是说你不需要为视图模型指定它吗?@StephenMuecke
  • 如果你有一个视图模型,那么没有(你已经防止过度发布攻击,因为你只映射你想要的数据模型)。为什么要创建两次数据模型? - 它可以只是db.Applicants.Add(applicant);db.ApplicantNotifcations.Add(applicantNotification),因为您已经将视图模型映射到数据模型的实例

标签: asp.net asp.net-mvc entity-framework asp.net-mvc-5


【解决方案1】:

感谢cmets中的建议。

看来,如果 datetime 列设置为允许为 null,则必须将 datetime 设置为 null 或设置为正确的格式才能使 sql datetime 工作。否则它会抛出

"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\The statement has been terminated."

https://stackoverflow.com/questions/4608734/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-o

我在我的实体对象中将日期设置为空,并在数据库中输入了一个新条目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多