C# 如何在asp.NETMVC4中一次点击两个表id保存到第三个表中

C# 如何在asp.NETMVC4中一次点击两个表id保存到第三个表中,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我是在asp.NETMVC4C中创建工作门户的,我在申请工作时遇到了问题 我正在创建三个表格:求职者、PostJobs和ApplyJobs 求职表 PostJobs表 应用作业表 单击应用按钮,在ApplyJob表中保存用户ID和采购订单ID当前用户ID 你的问题不太清楚 如果您担心求职者的Id和自动生成的PostJob表 或者做类似的事情 Jobseeker js = new Jobseeker(); .... //assign your property to js instance ...

我是在asp.NETMVC4C中创建工作门户的,我在申请工作时遇到了问题 我正在创建三个表格:求职者、PostJobs和ApplyJobs

求职表

PostJobs表

应用作业表


单击应用按钮,在ApplyJob表中保存用户ID和采购订单ID当前用户ID

你的问题不太清楚

如果您担心求职者的Id和自动生成的PostJob表

或者做类似的事情

Jobseeker js = new Jobseeker();
....
//assign your property to js instance
....
PostJob pj = new PostJob();
....
//assign your property to pj instance
....
ApplyJob aj = new ApplyJob();
aj.jobseeker = js; // assign it to navigation property and ef will handle ids
aj.Postjobs = pj; // assign it to navigation property and ef will handle ids
//Add to dbsets here
Context.SaveChanges();
暗示

您不需要第三个表,因为它看起来像多对多关系。因此,如果您不想在第三个表中处理插入,您可以通过在JobPost中声明ICollection和在Jobseeker类中声明ICollection来避免,这将强制您创建第三个表,该表将由EF维护和操作

这里有几个链接供你参考


所以,问题是如何在表中插入行还是什么?不清楚您在问什么!
public class PostJob
{
    [Key]
    public long po_ID { get; set; }
    [Required]
    public long emp_ID { get; set; }
    [Required]
    [Display(Name = "Company Name:")]
    public string comp_name { get; set; }
    [Required]
    [Display(Name = "Job Location:")]
    public string job_loc { get; set; }
    [Required]
    [Display(Name = "Address:")]
    public string adress { get; set; }
    [Required]
    [Display(Name = "Country:")]
    public string country { get; set; }
    [Required]
    [Display(Name = "City:")]
    public string city { get; set; }
    [Required]
    [Display(Name = "State:")]
    public string state { get; set; }
     [Required]
    [Display(Name = "Desiganation:")]
    public string dsg { get; set; }
    [Required]
    [Display(Name = "Salary:")]
    public int salary { get; set; }
    [Required]
    [Display(Name = "Experiance:")]
    public string Experiance { get; set; }
    [Required]
    [Display(Name = "Job Description:")]
    public string Description { get; set; }
    [Required]
    [Display(Name = "Skills:")]
    public string skill { get; set; }
    [Required]
    [Display(Name = "Post Date:")]
    [DataType(DataType.Date)]
    public DateTime post_dt { get; set; }
    //[Required]
    //[Display(Name = "Functional Area:")]
    //public long a_ID { get; set; }


    //public virtual Area Areafk { get; set; }
    public virtual empprofile empprofilefk { get; set; }
}       
public class ApplyJob
{
    [Key]
    public int a_Id { get; set; }

    public int u_ID { get; set; }

    public long po_ID { get; set; }

    public virtual Jobseeker jobseeker { get; set; }

    public virtual PostJob Postjobs { get; set; }
}        
Jobseeker js = new Jobseeker();
....
//assign your property to js instance
....
PostJob pj = new PostJob();
....
//assign your property to pj instance
....
Context.PostJobs.Add(pj);
Context.Jobseekers.Add(js);
Context.SaveChanges();
ApplyJob aj = new ApplyJob();
aj.u_ID = js.Id; // after instance is saved to db EF will populate Id in instance
aj.po_ID = pj.Id; // after instance is saved to db EF will populate Id in instance
Context.ApplyJobs.Add(aj);
Context.SaveChanges();
Jobseeker js = new Jobseeker();
....
//assign your property to js instance
....
PostJob pj = new PostJob();
....
//assign your property to pj instance
....
ApplyJob aj = new ApplyJob();
aj.jobseeker = js; // assign it to navigation property and ef will handle ids
aj.Postjobs = pj; // assign it to navigation property and ef will handle ids
//Add to dbsets here
Context.SaveChanges();