Asp.net 无法保存实体一对一EF mvc 5
我正在尝试向数据库中插入一条新记录,没有错误,申请者和申请者通知表中没有创建新记录。不知道我做错了什么 申请人Asp.net 无法保存实体一对一EF mvc 5,asp.net,asp.net-mvc,entity-framework,asp.net-mvc-5,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我正在尝试向数据库中插入一条新记录,没有错误,申请者和申请者通知表中没有创建新记录。不知道我做错了什么 申请人 [Index] [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int ApplicantID { get; set; } [Required] public string ApplicantTitle { get; set; }
[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);
}
感谢评论中的建议 如果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."
我在实体对象中将日期设置为null,并在数据库中输入了一个新条目。为什么要检查model.isValid twiceNo related,但您使用的是视图模型。删除那个可怕的
[Bind]
attribute@hasan很抱歉,这是一个输入错误,我的理解是在那里有绑定属性,以防止套印?你是说你不需要为viewmodel指定它吗?@StephenMueckei如果你有一个视图模型,那么就没有了(你的视图模型已经受到了过度发布攻击的保护,因为你只将你想要的映射到数据模型)。为什么要创建两次数据模型可以是db.applications.Add(applicator)
和db.applicationnotifications.Add(applicationnotification)
因为您已经将视图模型映射到数据模型的实例