在实体框架中添加新记录时未自动设置外键

时间:2019-04-23 11:31:08

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

我是实体框架中的新成员,我正在尝试在具有外键约束的表中添加记录。我有两个类,一个是用户,另一个是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();
        }
    }

1 个答案:

答案 0 :(得分:0)

在文件移动类中看不到任何选择的用户。您仅保存文件移动对象。尝试在控制器代码中明确设置用户ID,以查看所需结果。