Asp.net EntityFrameworkCore表关系

时间:2017-09-28 10:43:48

标签: asp.net

我在ASP.NET核心中编写了以下代码。我想在Post列之上的表Interviewinterview_id之间建立连接。我怎么能这样做?

public class Post { 
    [Display(Name = "Post Title")]
    [Required]
    [DataType(DataType.Text)]
    public long interview_id { get; set; }
    public int visa_type_id { get; set; }
    public DateTime Posted { get; set; }
    public string story { get; set; }
    public bool gotten { get; set; }
}

public class Interview {
    [Display(Name = "Post Title")]
    [Required]
    [DataType(DataType.Text)]
    public long Id { get; set; }
    public int interview_id { get; set; }
    public DateTime Posted { get; set; }
    public string question { get; set; }
    public string answer { get; set; }
}

1 个答案:

答案 0 :(得分:0)

如果您想要一对一的关系,您只需在表类中添加相关实体:

public class Post
{
    public long interview_id { get; set; }
    public Interview Interview { get; set; }
}

// if you want two way relation
public class Interview
{
    public long post_id { get; set; }
    public Post Post { get; set; }
}