简体   繁体   中英

Foreign key is not setting automatically while adding new record in entity framework

I am new in entity framework i am trying to add a record in table with foreign key constrain. i have two classes one is user and other is FileMovement. Filemovement have userId as foreign key. New record added successfully but foreign key value remain null. can any one tell me whats wrong i have done here is my code.

my user class code

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

and here is my fileMovement class code

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

and here is my actionResult method code

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

and here is SaveNewFileMOvement method

    public void SaveNewFileMovement(File_Movement fm)
    {
        using (DiaryManagementSystemEntities db = new DiaryManagementSystemEntities())
        {

            db.File_Movement.Add(fm);
            db.SaveChanges();
        }
    }

I cannot see any selection of user in your file movement class. You are saving just file movement object. Try setting User Id explicitly in your controller code to see the desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM