Я новичок в структуре сущностей. Я пытаюсь добавить запись в таблицу с ограничением внешнего ключа. У меня есть два класса, один пользователь и другой FileMovement. Filemovement имеет userId в качестве внешнего ключа. Новая запись успешно добавлена, но значение внешнего ключа остается нулевым. может кто-нибудь сказать мне, что я сделал не так, вот мой код.
мой код класса пользователя
[Key]
public int User_ID { get; set; }
[Display(Name = " User Nmae: ")]
[Required(ErrorMessage = "User Name is required")]
public string User_Name { get; set; }
[Display(Name = " Password: ")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required")]
public string Password { get; set; }
и вот мой код класса fileMovement
[Key]
public int file_Id { get; set; }
[ForeignKey("User")]
public int User_Id_fk { get; set; }
[Display(Name = " Diary No: ")]
[Required(ErrorMessage = "Diary No is required")]
public string Diary_No { get; set; }
[Display(Name = " Receive Date: ")]
[Required(ErrorMessage = "Please enter receive date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Receive_Date { get; set; }
[Display(Name = " Issued Diary No: ")]
public string Issued_Diary_No { get; set; }
[Display(Name = " Issue Date: ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Issue_Date { get; set; }
[Display(Name = " Office Name: ")]
public string Issued_Office { get; set; }
[Display(Name = " Subject: ")]
[Required(ErrorMessage = "Please enter subject")]
[DataType(DataType.MultilineText)]
public string Subject { get; set; }
[Display(Name = " DG(Rectt): ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> DG_Rectt { get; set; }
[Display(Name = " ADG(Rectt): ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> ADG_Rectt { get; set; }
[Display(Name = " DD(Rectt): ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> DD_Rectt { get; set; }
[Display(Name = " AD(Rectt): ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> AD_Rectt { get; set; }
[Display(Name = " Assistant: ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Assistant_Rectt { get; set; }
[Display(Name = " GM(HRD): ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> GM_HRD { get; set; }
[Display(Name = " Send To: ")]
public string Send_To { get; set; }
[Display(Name = " Date: ")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> Date { get; set; }
[Display(Name = " Action: ")]
public string Action { get; set; }
[Display(Name = " Status: ")]
public string Status { get; set; }
и вот мой код метода actionResult
public ActionResult NewFileToDiary(File_Movement fm)
{
if (ModelState.IsValid)
{
FileMovementManagementSystem.FileViewModel.FileViewModel fvm = new FileMovementManagementSystem.FileViewModel.FileViewModel();
fvm.SaveNewFileMovement(fm);
return RedirectToAction("UserDashBoard", "Home");
}
return View();
}
и вот метод SaveNewFileMOvement
public void SaveNewFileMovement(File_Movement fm)
{
using (DiaryManagementSystemEntities db = new DiaryManagementSystemEntities())
{
db.File_Movement.Add(fm);
db.SaveChanges();
}
}